ng-watch directive

This commit is contained in:
Misko Hevery 2010-03-22 16:07:42 -07:00
parent d4ba33d075
commit 6f8276a8e3
3 changed files with 27 additions and 11 deletions

View file

@ -207,6 +207,10 @@ Scope.prototype = {
},
addWatchListener: function(watchExpression, listener) {
// TODO: clean me up!
if (!isFunction(listener)) {
listener = bind(this, this.compile(listener), {scope: this, self: this.state});
}
var watcher = this.watchListeners[watchExpression];
if (!watcher) {
watcher = {listeners:[], expression:watchExpression};

View file

@ -82,6 +82,17 @@ angularDirective("ng-repeat", function(expression, element){
};
}, {exclusive: true});
angularDirective("ng-watch", function(expression, element){
var match = expression.match(/^([^.]*):(.*)$/);
if (!match) {
throw "Expecting watch expression 'ident_to_watch: watch_statement' got '"
+ expression + "'";
}
return function(){
this.$watch(match[1], match[2]);
};
});
/////////////////////////////////////////
/////////////////////////////////////////
@ -109,17 +120,6 @@ angularDirective("action", function(expression, element){
};
});
//ng-watch
// <div ng-watch="$anchor.book: book=Book.get();"/>
angularDirective("watch", function(expression, element){
var watches = {
'lhs':'rhs'
}; // parse
return function(){
this.$watch(watches);
};
});
//widget related
//ng-validate, ng-required, ng-formatter
//ng-error

View file

@ -82,4 +82,16 @@ describe("directives", function(){
});
expect(log).toEqual("\"Expected ng-repeat in form of 'item in collection' but got 'i dont parse'.\";true;");
});
it('should ng-watch', function(){
var scope = compile('<div ng-watch="i: count = count + 1" ng-init="count = 0">');
scope.updateView();
scope.updateView();
expect(scope.get('count')).toEqual(0);
scope.set('i', 0);
scope.updateView();
scope.updateView();
expect(scope.get('count')).toEqual(1);
});
});