【发布时间】:2015-06-23 07:14:39
【问题描述】:
我在项目范围内的一些角度变量上使用$scope.$watch 时遇到问题。我制作了一个示例jsfiddle 来说明问题。
基本上,我可以$scope.$watch ng-switch 中的任何模型,只要它是一个对象。如果它是一个字符串,它不会触发监视表达式,并且不会在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);
});
【问题讨论】:
标签: angularjs angularjs-scope ng-repeat ng-switch