Angular Modal UI中的变量未被观看

时间:2018-01-09 13:23:53

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

In this plunk我有一个Angular Modal UI,其中包含一个被监视的变量,但是当变量被更改时,不会触发监视功能,这有什么不对?

的Javascript

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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

刚刚在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 =&#34; path / to / jsfile.js&#34;加上所述控制器的名称。