我在使用$scope.$watch
项目范围内的一些角度变量时遇到问题。我制作了一个示例jsfiddle来说明这个问题。
基本上,只要是对象,我就可以$scope.$watch
在其中的任何模型。ng-switch
如果是字符串,则不会触发 watch 表达式,并且不会在ng-switch
区域范围之外更改。Angular 是否复制原始字符串而不是像对象一样通过引用传递它?
我使用ng-repeat
了一些元素——这是我的一些代码:
<div ng-switch="key">
<div ng-switch-when="deal_id">
DEALID: <input type="text" ng-model="dealIdModel"/> -- {{dealIdModel}}
</div>
<div ng-switch-when="thing_id">
THING: <input type="text" ng-model="thingIdModel.test"/> -- {{thingIdModel.test}}
</div>
<div ng-switch-default>
DEFAULT: <input type="text" placeholder="email" ng-model="items.params.email"/> -- {{items.params.email}}
</div>
</div>
和 JS:
$scope.items = {
"statusCode":"ACTIVE",
"params":{
"deal_id":"",
"thing_id":"",
"email":"Change me! I will get called"
}
};
$scope.dealIdModel = "I won't change outside of the loop or get called.";
$scope.thingIdModel = {test:'Change me! So will I!'};
$scope.$watch('items.params.email', function (now, then, scope) {
console.log('email change', now, then);
});
$scope.$watch('thingIdModel.test', function(now, then, scope) {
console.log('changed the thing', now, then);
});
$scope.$watch('dealIdModel', function(now, then, scope) {
console.log('dealID changed:', now, then);
});