UI Bootstrap模态承诺和控制器范围

时间:2017-02-11 14:17:59

标签: javascript angularjs angular-ui-bootstrap angular-ui-modal

我是AngularJS的新手并在项目中使用UI Bootstrap。一旦模态(包含T& Cs文本)关闭(但在模态被解除时不是),我想标记我已经读过T& Cs 复选框。

我已在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);
  };
});

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分: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" />