-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix($compile): do not initialize optional '&' binding if attribute not specified #9216
Conversation
So, the error content isn't very good, I'm not sure what else to add to it really. I feel like this will surprise people, which isn't very nice :( Also it's another breaking change, so we probably can't really get this into 1.3. However it has been asked for, and it probably is the expected behaviour. /cc @IgorMinar can you take a look and decide if we want to do this or not? |
@description | ||
|
||
If a directive requires an attribute to be present, by specifying a non-optional isolate scope binding for that attribute, and the attribute | ||
not present, this error is thrown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is not present
I think this can cause more than one WTF moment... that said, what the patch does is closer to what it is expected... Can this be made an opt-in? |
we probably have to make it opt-in, otherwise it's a breaking change :( will see if we even want to do this though |
The problem with opt-in is, nobody will ever opt into it. The problem with not opt-in is, it breaks apps. It's an endless struggle .v. |
it's too late for this change to get into 1.3. making this an opt-in is not a great option because as soon as you come across a third-party component that doesn't specify some required attributes in its template you have to opt-out. I do agree that it's a good change though. |
e8dc429
to
e83fab9
Compare
4dd5a20
to
998c61c
Compare
It will break a lot of existing directives... What is profit of that change? If attribute is not marked as optional but in code of directive it is optional (sometimes even with checks, if it's function in scope.variable) - it will work. |
we all think so --- but it is what it is |
CLAs look good, thanks! |
@caitp - I am also feeling uncomfortable about this change.
This way, we do not break existing directives; plus we provide a way for directive developers to specify optional expressions and easily check for their existence in the template, etc. What do you think? (cc @IgorMinar) |
updated, PTAL |
it('should not initialize scope value if optional expression binding with Object.prototype name is not passed', inject(function($compile) { | ||
compile('<div my-component></div>'); | ||
var isolateScope = element.isolateScope(); | ||
expect(isolateScope.constructor).toBe($rootScope.constructor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Object.prototype stuff is optional --- I mean, it's basically going to do the wrong thing no matter what, so it's kind of weird. One option is to just always assign to undefined instead of just breaking, I guess?
Great - taking a look! |
…t specified BREAKING CHANGE: Previously, '&' expressions would always set up a function in the isolate scope. Now, if the binding is marked as optional and the attribute is not specified, no function will be added to the isolate scope. Closes angular#6404
parentGet = $parse(attrs[attrName]); | ||
|
||
// Don't assign noop to destination if expression is not valid | ||
if (parentGet.noop && optional) break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will only happen if a non-string/function is passed to $parse
. When would that happen?
Not so easy to cherry pick to 1.3.x ... |
i'm not sure we should backport it |
I realise after trying to do so manually that it is effectively a breaking change. So yes, you are right |
Sorry for digging out an old thread. I noticed that there is a breaking change regarding This example throws with an exception when you try to toggle the second zippy because it doesn't use the http://plnkr.co/edit/Ef2iKOde3TmGzJHRGEhE?p=preview The same example using http://plnkr.co/edit/cWxO6shBz7OaiewCDMHp?p=preview /cc @PascalPrecht |
That is weird @cburgdorf - can you open a new issue for it. I think it is a bug. |
Thanks for the fast reply! I opened this one #13367 |
Also, do not set up an expression in scope if the '&' binding is optional.
BREAKING CHANGE:
Previously, '&' expressions would always set up a function in the isolate
scope. Now, if the binding is marked as optional and the attribute is not
specified, no function will be added to the isolate scope.
Finally, if the '&' expression is not optional, and the attribute is not
specified, then rather than the function being essentially a NOOP, it will
instead throw an error indicating to the programmer that a required attribute
was not specified.
Closes #6404