在我的一个角度1.x控制器中,我有以下watch
语句
$scope.$watch($scope.someData.timestamp, doSomething);
timestamp
属性为null
,undefined
或格式为string
的时间戳,如" 2016-05-31T09:34 :01.61Z"
执行watch
语句时,出现以下错误:
angular.js:11655 Error: [$parse:syntax] Syntax Error: Token 'T09' is an unexpected token at column 11 of the expression [2016-05-31T09:34:01.61Z] starting at [T09:34:01.61Z]. http://errors.angularjs.org/1.3.15/$parse/syntax?p0=T09&p1=is%20an%20unexpected%20token&p2=11&p3=2016-05-31T09%3A34%3A01.61Z&p4=T09%3A34%3A01.61Z
at https://localhost:44555/bower_components/angular/angular.js:63:12
at Parser.throwError (https://localhost:44555/bower_components/angular/angular.js:12070:11)
at Parser.parse (https://localhost:44555/bower_components/angular/angular.js:12023:12)
at $parse (https://localhost:44555/bower_components/angular/angular.js:12737:39)
at Scope.$watch (https://localhost:44555/bower_components/angular/angular.js:13897:19)
at initialize (https://localhost:44555/app/MyCtrl.js:49:20)
at new MyCtrl (https://localhost:44555/app/MyCtrl.js:18:9)
at invoke (https://localhost:44555/bower_components/angular/angular.js:4203:17)
at Object.instantiate (https://localhost:44555/bower_components/angular/angular.js:4211:27)
at https://localhost:44555/bower_components/angular/angular.js:8501:28 <ui-view class="ng-scope">
对我而言,看起来像angular正在尝试编译和评估属性值。但为什么呢?
如果我更改watch
语句,请改用函数语法,一切都按预期工作:
$scope.$watch(function(){return $scope.someData.timestamp; }, doSomething);
为什么要解析值?
如何阻止对已监视的属性进行解析?
答案 0 :(得分:2)
根据doc,变量名称应该在引号内,如下所示:
$scope.$watch('someData.timestamp', function(){...})
答案 1 :(得分:0)
你试过了吗?
$ scope。$ watch(someData.timestamp,doSomething);
您应该使用$ watchCollection来观察对象更改,或者使用$ watch来观察范围属性。