Skip to content

Commit 368ce25

Browse files
committed
remove shallow watch, update docs
1 parent e141438 commit 368ce25

File tree

2 files changed

+20
-96
lines changed

2 files changed

+20
-96
lines changed

src/ng/compile.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,28 +186,30 @@
186186
* you want to shallow watch for changes (i.e. $watchCollection instead of $watch) you can use
187187
* `=*` or `=*attr` (`=*?` or `=*?attr` if the property is optional).
188188
*
189-
* * `<` or `<attr` - set up one-way (one-directional) binding between a local scope property and the
190-
* parent scope property of name defined via the value of the `attr` attribute. If no `attr`
191-
* name is specified then the attribute name is assumed to be the same as the local name.
192-
* Given `<dir my-attr="parentModel">` and directive definition of
193-
* `scope: { localModel:'<myAttr' }`, then isolate scope property `localModel` will reflect the
189+
* * `<` or `<attr` - set up a one-way (one-directional) binding between a local scope property and an
190+
* expression passed via the attribute `attr`. The expression is evaluated in the context of the
191+
* parent scope. If no `attr` name is specified then the attribute name is assumed to be the same as the
192+
* local name. You can also make the binding optional by adding `?`: `<?` or `<?attr`.
193+
*
194+
* For example, given `<dir my-attr="parentModel">` and directive definition of
195+
* `scope: { localModel:'<myAttr' }`, then the isolated scope property `localModel` will reflect the
194196
* value of `parentModel` on the parent scope. Any changes to `parentModel` will be reflected
195197
* in `localModel`, but changes in `localModel` will not reflect in `parentModel`. There are however
196198
* two caveats:
197199
* 1. one-way binding does not copy the value from the parent to the isolate scope, it simply
198200
* sets the same value. That means if your bound value is an object, changes to its properties
199-
* in the isolate scope will be reflected in the parent scope.
200-
* 2. one-way binding watches changes to the **identity** of the parent value. That is important should
201-
* you one-way bind an object, and then replace that object in the isolated scope. If you now change
202-
* a property of the object in your parent scope, the change will not be propagated to the isolated
203-
* scope, because the identity of the object has not changed. Instead you must assign a new object.
201+
* in the isolated scope will be reflected in the parent scope (because both reference the same object).
202+
* 2. one-way binding watches changes to the **identity** of the parent value. That means the
203+
* {@link ng.$rootScope.Scope#$watch`$watch`} on the parent value only fires if the reference
204+
* to the value has changed. In most cases, this should not be of concern, but can be important
205+
* to know if you one-way bind to an object, and then replace that object in the isolated scope.
206+
* If you now change a property of the object in
207+
* your parent scope, the change will not be propagated to the isolated scope, because the identity
208+
* of the object has not changed. Instead you must assign a new object.
204209
*
205210
* One-way binding is useful if you do not plan to propagate changes to your isolated scope bindings
206211
* back to the parent. However, it does not make this completely impossible.
207212
*
208-
* Same as with bi-directional bindings, you can also use shallow watch for changes (i.e. $watchCollection instead of $watch):
209-
* `<*` or `<*attr` (`<*?` or `<*?attr` if the property is optional).
210-
*
211213
* * `&` or `&attr` - provides a way to execute an expression in the context of the parent scope.
212214
* If no `attr` name is specified then the attribute name is assumed to be the same as the
213215
* local name. Given `<widget my-attr="count = count + value">` and widget definition of
@@ -848,7 +850,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
848850
var EVENT_HANDLER_ATTR_REGEXP = /^(on[a-z]+|formaction)$/;
849851

850852
function parseIsolateBindings(scope, directiveName, isController) {
851-
var LOCAL_REGEXP = /^\s*([@&]|[=<](\*?))(\??)\s*(\w*)\s*$/;
853+
var LOCAL_REGEXP = /^\s*([@&<]|=(\*?))(\??)\s*(\w*)\s*$/;
852854

853855
var bindings = {};
854856

@@ -3064,15 +3066,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
30643066

30653067
destination[scopeName] = parentGet(scope);
30663068

3067-
if (definition.collection) {
3068-
removeWatch = scope.$watchCollection(parentGet, function parentCollectionValueWatchAction(newParentValue) {
3069-
destination[scopeName] = newParentValue;
3070-
});
3071-
} else {
3072-
removeWatch = scope.$watch(parentGet, function parentValueWatchAction(newParentValue) {
3073-
destination[scopeName] = newParentValue;
3074-
}, parentGet.literal);
3075-
}
3069+
removeWatch = scope.$watch(parentGet, function parentValueWatchAction(newParentValue) {
3070+
destination[scopeName] = newParentValue;
3071+
}, parentGet.literal);
3072+
30763073
removeWatchCollection.push(removeWatch);
30773074
break;
30783075

test/ng/compileSpec.js

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,8 +3537,6 @@ describe('$compile', function() {
35373537
owRefAlias: '< owRef',
35383538
owOptref: '<?',
35393539
owOptrefAlias: '<? owOptref',
3540-
owColref: '<*',
3541-
owColrefAlias: '<* owColref',
35423540
expr: '&',
35433541
optExpr: '&?',
35443542
exprAlias: '&expr',
@@ -4479,77 +4477,6 @@ describe('$compile', function() {
44794477
expect(componentScope.owOptrefAlias).toBe(undefined);
44804478
}));
44814479
});
4482-
4483-
describe('collection object reference', function() {
4484-
it('should update isolate scope when origin scope changes', inject(function() {
4485-
$rootScope.collection = [{
4486-
name: 'Gabriel',
4487-
value: 18
4488-
}, {
4489-
name: 'Tony',
4490-
value: 91
4491-
}];
4492-
$rootScope.query = "";
4493-
$rootScope.$apply();
4494-
4495-
compile('<div><span my-component ow-colref="collection | filter:query">');
4496-
4497-
expect(componentScope.owColref).toEqual($rootScope.collection);
4498-
expect(componentScope.owColrefAlias).toEqual(componentScope.owColref);
4499-
4500-
$rootScope.query = "Gab";
4501-
$rootScope.$apply();
4502-
4503-
expect(componentScope.owColref).toEqual([$rootScope.collection[0]]);
4504-
expect(componentScope.owColrefAlias).toEqual([$rootScope.collection[0]]);
4505-
}));
4506-
4507-
4508-
it('should not update the origin scope when isolate scope collection is replaced', inject(function() {
4509-
$rootScope.collection = [{
4510-
name: 'Gabriel',
4511-
value: 18
4512-
}, {
4513-
name: 'Tony',
4514-
value: 91
4515-
}];
4516-
4517-
compile('<div><span my-component ow-colref="collection">');
4518-
4519-
componentScope.owColref = [{
4520-
name: 'Boris',
4521-
value: 55
4522-
}];
4523-
componentScope.$apply();
4524-
4525-
expect($rootScope.collection[1]).toEqual({name: 'Tony', value: 91});
4526-
}));
4527-
4528-
4529-
it('should update the origin scope when isolate scope item changes', inject(function() {
4530-
$rootScope.collection = [{
4531-
name: 'Gabriel',
4532-
value: 18
4533-
}, {
4534-
name: 'Tony',
4535-
value: 91
4536-
}];
4537-
4538-
compile('<div><span my-component ow-colref="collection">');
4539-
4540-
var newItem = {
4541-
name: 'Pablo',
4542-
value: 10
4543-
};
4544-
componentScope.owColref[0].value = 72;
4545-
componentScope.owColref.push(newItem);
4546-
componentScope.$apply();
4547-
4548-
expect($rootScope.collection[0].value).toBe(72);
4549-
expect($rootScope.collection[2]).toEqual(newItem);
4550-
}));
4551-
4552-
});
45534480
});
45544481
});
45554482

0 commit comments

Comments
 (0)