1

我是 AngularJS 的新手,在项目中使用 UI Bootstrap。一旦模式(包含条款和条件文本)关闭(但不是在模式被解除时),我想将我已阅读条款和条件复选框标记为选中。

我已经在https://angular-ui.github.io/bootstrap/上修改了一般示例,但是在使用 $scope.accept_terms 的值更新范围/视图时遇到问题。

我的模式打开/关闭很好,但复选框始终未选中。

HTML:

<form>
    <div class="checkbox">
        <input type="checkbox" id="user_details_termsConditions"
               name="user_details[termsConditions]"
               required="required" ng-checked="accept_terms" />
        <label for="user_details_termsConditions" >I agree to the <a href="" ng-click="$ctrl.open('', '.modal-container')">terms and conditions</a></label>                 
    </div>
</form>

JS:

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($uibModal, $log, $document, $scope) {
  var $ctrl = this;

  $ctrl.animationsEnabled = true;

  $ctrl.open = function (size, parentSelector) {
    var parentElem = parentSelector ?
    angular.element($document[0].querySelector('.container ' + parentSelector)) : undefined;

    var modalInstance = $uibModal.open({
      animation: $ctrl.animationsEnabled,
      ariaLabelledBy: 'modal-title',
      ariaDescribedBy: 'modal-body',
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      controllerAs: '$ctrl',
      size: size,
      appendTo: parentElem
    });

    modalInstance.result.then(function (result) {
        $scope.accept_terms = result;
        $log.info('accept_terms = ' + $scope.accept_terms);
        // prints accept_terms = 1
    }, function () {

    });

  };
});

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance) {
  var $ctrl = this;

  $ctrl.ok = function () {
    $uibModalInstance.close(true);
  };
});

谁能指出我正确的方向?

4

1 回答 1

0

原来这是在复选框上使用 ng-model 而不是 ng-checked 的简单问题:

控制器:

modalInstance.result.then(function (result) {
    $scope.accept_terms = true;
}, function () {

});

标记:

<input type="checkbox" id="user_details_termsConditions"
       name="user_details[termsConditions]" required="required"
       ng-model="accept_terms" value="1" />
于 2017-02-12T15:22:14.050 回答