From b0ba1dc59063119841a2b0b69e683111f581d8af Mon Sep 17 00:00:00 2001 From: rodyhaddad Date: Mon, 14 Jul 2014 09:46:56 -0700 Subject: [PATCH] fix($rootScope): remove support for a watch action to be a string BREAKING CHANGE: Previously, it was possible for an action passed to $watch to be a string, interpreted as an angular expresison. This is no longer supported. The action now has to be a function. Passing an action to $watch is still optional. Before: ```js $scope.$watch('state', ' name="" '); ``` After: ```js $scope.$watch('state', function () { $scope.name = ""; }); ``` --- src/ng/rootScope.js | 6 ++---- test/BinderSpec.js | 4 +++- test/ng/rootScopeSpec.js | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 34f1fa6ada1a..f4b7be50e5bd 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -315,7 +315,7 @@ function $RootScopeProvider(){ * * - `string`: Evaluated as {@link guide/expression expression} * - `function(scope)`: called with current `scope` as a parameter. - * @param {(function()|string)=} listener Callback called whenever the return value of + * @param {function()=} listener Callback called whenever the return value of * the `watchExpression` changes. * * - `string`: Evaluated as {@link guide/expression expression} @@ -340,10 +340,8 @@ function $RootScopeProvider(){ lastDirtyWatch = null; - // in the case user pass string, we need to compile it, do we really need this ? if (!isFunction(listener)) { - var listenFn = compileToFn(listener || noop, 'listener'); - watcher.fn = function(newVal, oldVal, scope) {listenFn(scope);}; + watcher.fn = noop; } if (!array) { diff --git a/test/BinderSpec.js b/test/BinderSpec.js index 23519fe8fc17..f00365cceff8 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -442,7 +442,9 @@ describe('Binder', function() { it('ItShouldFireChangeListenersBeforeUpdate', inject(function($rootScope, $compile) { element = $compile('
')($rootScope); $rootScope.name = ''; - $rootScope.$watch('watched', 'name=123'); + $rootScope.$watch('watched', function () { + $rootScope.name = 123; + }); $rootScope.watched = 'change'; $rootScope.$apply(); expect($rootScope.name).toBe(123); diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index adafaed5d8ae..8d9ce00dfaa4 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -1143,7 +1143,9 @@ describe('Scope', function() { it('should cause a $digest rerun', inject(function($rootScope) { $rootScope.log = ''; $rootScope.value = 0; - $rootScope.$watch('value', 'log = log + ".";'); + $rootScope.$watch('value', function () { + $rootScope.log = $rootScope.log + "."; + }); $rootScope.$watch('init', function() { $rootScope.$evalAsync('value = 123; log = log + "=" '); expect($rootScope.value).toEqual(0);