未监视角度模式 UI 中的变量



在这个 plunk 中,我有一个带有被监视变量的 Angular Modal UI,但是当变量更改时不会触发监视功能,这是怎么回事?

爪哇语

var app = angular.module('app', ['ui.bootstrap']);
app.controller('ctl', function ($scope,$uibModal) {
        $scope.x = 'a';
        $scope.open = function(){
          $scope.modalInstance = $uibModal.open({
              templateUrl: 'myModalContent.html',
              scope: $scope
            }); 
        };
        $scope.close = function(){
          $scope.modalInstance.close();
        };

        $scope.$watch('x', function (newValue, oldValue) {
          if (typeof newValue === 'undefined')
              return;
           alert('value='+$scope.newValue);
       });
});

.HTML

<button ng-click="open()">Open</button>
<script type="text/ng-template" id="myModalContent.html">
   <div class="modal-header">
      <h4 class="modal-title">The Title</h4>
      value <input type="text" ng-model="x" />
      <button ng-click="close()">Close</button>
   </div>
</script>

刚刚在 plunker 中测试过,$uibModal需要您的控制器才能绑定到您的范围。

改变;

$scope.open = function(){
      $scope.modalInstance = $uibModal.open({
          templateUrl: 'myModalContent.html',
          scope: $scope
        }); 
    };

自;

$scope.open = function(){
      $scope.modalInstance = $uibModal.open({
          templateUrl: 'myModalContent.html',
          scope: $scope,
          controller: "ctl"
        }); 
    };

如果您为其使用 .js 文件,则可以使用 controllerUrl = "path/to/jsfile.js" 加上所述控制器的名称。

请在 $uibModal 中定义控制器.open 以获取更多参考,请访问此文档的详细信息https://github.com/angular-ui/bootstrap/tree/master/src/modal/docs

这个问题有助于解决$uibModal加载事件

刚刚添加到modalController

$scope.open = function () {
    $scope.status.opened = true;
};
$scope.$watch(status, function (newValue, oldValue) {
    //code required onload..
});

最新更新