$腕表不触发数据变化 [英] $watch not firing on data change
问题描述
我对一个用户界面,选择2下拉(从UI的引导)的模型手表设置。负载但不能在数据变化,我想不通为什么手表火灾。
I have a watch setup against the model of a ui-select2 dropdown (from ui-bootstrap). The watch fires on load but not on data changes and I can't figure out why.
这不是没有$ apply'ing模式改变或不使用相等比较的第三个参数(至少从我的code)通常的问题。
It isn't the usual problem of not $apply'ing the model change or not using the third parameter for equality comparison (at least from my code).
什么我需要做的就是它火?
What do I need to do to get it to fire?
推荐答案
我固定的一些东西。
<一个href=\"http://plnkr.co/edit/5Zaln7QT2gETVcGiMdoW?p=$p$pview\">http://plnkr.co/edit/5Zaln7QT2gETVcGiMdoW?p=$p$pview
在JS
var myMod = angular.module("myApp",[]).controller("MainController", function($scope){
$scope.myModel = {selectedId:null};
}).controller("DetailController",function($scope){
$scope.items = [1,2,3,4];
$scope.watchHitCount = 0;
$scope.$watch('myModel.selectedId', function(newVal, oldVal){
console.log(newVal + " " + oldVal);
$scope.watchHitCount++;
},true);
});
中的index.html
The index.html
<body ng-app="myApp">
<div ng-controller="MainController">
<ng-include src="'detail.html'" ng-controller="DetailController"></ng-include>
</div>
</body>
该detail.html
The detail.html
<pre>watch hit: {{watchHitCount}}</pre>
<pre>selected value: {{myModel.selectedId}}</pre>
<select ng-model="myModel.selectedId" ui-select2="">
<option></option>
<option ng-repeat="item in items" value="{{item}}">{{item}}</option>
</select>
有人抱怨找不到控制器,所以我将它设置的方式,我通常会与一个名为NG-app并在其上有定义控制器声明的模块。
It was complaining about not finding the controller so I set it up the way I normally would with a named ng-app and a module declared that has controllers defined on it.
我还添加了一个对象来保存你的模型的价值。这是不好的做法,使用$范围对象模型,而不是你的范围应指向一个对象,它是你的模型。
I also added an object to hold the value in your model. It is bad practice to use the $scope object as your model, instead your scope should refer to an object that is your model.
这篇关于$腕表不触发数据变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!