Skip to content

Commit

Permalink
fix($rootScope): remove support for a watch action to be a string
Browse files Browse the repository at this point in the history
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 angular#8190
  • Loading branch information
rodyhaddad committed Jul 14, 2014
1 parent 3dafcba commit 02c0ed2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion test/BinderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ describe('Binder', function() {
it('ItShouldFireChangeListenersBeforeUpdate', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind="name"></div>')($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);
Expand Down
4 changes: 3 additions & 1 deletion test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 02c0ed2

Please sign in to comment.