Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 02c0ed2

Browse files
committed
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 = ""; }); ``` Closes #8190
1 parent 3dafcba commit 02c0ed2

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/ng/rootScope.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ function $RootScopeProvider(){
315315
*
316316
* - `string`: Evaluated as {@link guide/expression expression}
317317
* - `function(scope)`: called with current `scope` as a parameter.
318-
* @param {(function()|string)=} listener Callback called whenever the return value of
318+
* @param {function()=} listener Callback called whenever the return value of
319319
* the `watchExpression` changes.
320320
*
321321
* - `string`: Evaluated as {@link guide/expression expression}
@@ -340,10 +340,8 @@ function $RootScopeProvider(){
340340

341341
lastDirtyWatch = null;
342342

343-
// in the case user pass string, we need to compile it, do we really need this ?
344343
if (!isFunction(listener)) {
345-
var listenFn = compileToFn(listener || noop, 'listener');
346-
watcher.fn = function(newVal, oldVal, scope) {listenFn(scope);};
344+
watcher.fn = noop;
347345
}
348346

349347
if (!array) {

test/BinderSpec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ describe('Binder', function() {
442442
it('ItShouldFireChangeListenersBeforeUpdate', inject(function($rootScope, $compile) {
443443
element = $compile('<div ng-bind="name"></div>')($rootScope);
444444
$rootScope.name = '';
445-
$rootScope.$watch('watched', 'name=123');
445+
$rootScope.$watch('watched', function () {
446+
$rootScope.name = 123;
447+
});
446448
$rootScope.watched = 'change';
447449
$rootScope.$apply();
448450
expect($rootScope.name).toBe(123);

test/ng/rootScopeSpec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,9 @@ describe('Scope', function() {
11431143
it('should cause a $digest rerun', inject(function($rootScope) {
11441144
$rootScope.log = '';
11451145
$rootScope.value = 0;
1146-
$rootScope.$watch('value', 'log = log + ".";');
1146+
$rootScope.$watch('value', function () {
1147+
$rootScope.log = $rootScope.log + ".";
1148+
});
11471149
$rootScope.$watch('init', function() {
11481150
$rootScope.$evalAsync('value = 123; log = log + "=" ');
11491151
expect($rootScope.value).toEqual(0);

0 commit comments

Comments
 (0)