【发布时间】:2021-11-25 20:47:20
【问题描述】:
In this plunk我有一个Angular Modal UI,其中有一个被监视的变量,但是当变量改变时没有触发watch函数,这是怎么回事?
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>
【问题讨论】:
-
尝试将控制器添加到模态实例中,例如 $uibModal.open({ templateUrl: "myModalContent.html", scope: $scope, controller: "ctl" })
-
谢谢,解决了问题
标签: angularjs angular-ui-bootstrap angular-ui