You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
directives with isolated scope and a optional function '&?' should be returned as undefined if the function is missing #6404
It is not a bug, probably just a design issue about directives with isolated scope and optional functions passed via '&?'.
If you don't pass any function to the directive, you get a function that returns undefined instead of undefined.
Errors
No errors are thrown.
Motivation for or Use Case
If you declare a scope property as optional, probably the expected behaviour is getting undefined if you try to access this property.
This is exactly what happens when you have a '@?' binding and you don't fill in the optional attribute: you get undefined trying to access $scope.property.
The way to understand if there is a function parameter is inspecting the attrs.
So this seems a special case not motivated for '&?'.
Angular Version(s)
All versions, no regression.
Browsers and Operating System
All browsers and operting systems.
Suggestion
In order to get a common behaviour for optional parameters you could just add the following control to
Thanks for this! I think this will take some serious consideration since changes to $compile tend to have far-reaching effects and unintended consequences.
I don't have a particularly strong opinion on this.
Right now, the compiler doesn't really track whether or not an expression makes a function call or not (or how many, as it could be more than one). I don't feel like this really makes a lot of sense.
If we did force an expression to do something meaningful (for some definition of "meaningful", by throwing if it doesn't), this would break some apps.
I guess it's a tradeoff between notifying people that they might be doing something they don't expect to do, but I think that given how hard it is to tell if an expression is doing something useful, it's probably not a good idea to change this.
edit: It isn't that bad to make the "optional" flag actually do something for & properties, though. That seems okay with me.
edit again: actually, making these optional would still be a breaking change for directives using optional-flagged expression properties, because it would require the directive to test for the presence of the scope property before evaluating the expression. This seems bad.
Ok, how can I help you? I want to dive into angular and there are tickets that needs a PR, so I would be glad to contribute.
I just need a mentor that reviews my code
I think there should be a way to check if the function is passed in or not, right now it's just too weird. I have to manually check if the attribute exists.
Hi,
i still see this behaviour in Angular 1.5.8
i´m passing an optional function to a sub-sub-component with "&?" in both steps.
The first sub-component detects "undefined", but the sub-sub-component got a function instead.
In my sub-component's template i would like to avoid having some logic toggling between <sub-sub-component some-attribute="$ctrl.anAttribute">.......</...>
and <sub-sub-component some-attribute="$ctrl.anAttribute" optional-function="$ctrl.myFunction()">.....</...>
As a workaround i use instead "=" as shown bindings: { myFunction: "=" }
@sramilli I had the exact same issue today (1.5.8 too). Workaround I applied was the same too.
(Fun fact: I came to this thread again via Google, and then got freaked out just now because I got this notifcation. Wasn't until I visited GitHub to reply that I realised that I'd already participated in this issue over 2 years ago.)
@davidemoro@caitp I'd like to mention that we recently hit upon this issue during our upgrade from 1.3 to 1.5 and our unit tests did not capture this behavior because in 1.3 there was no difference in the outcome between using a directive without the optional attribute vs having an optional value that is undefined.
I think this should have been marked as a breaking change or at least had some note about potential breaking change.
I admit I didn't read the full change log line by line and only looked at the breaking changes section but still looking back I would have preferred some hint about this change.
Activity
btford commentedon Feb 22, 2014
Thanks for this! I think this will take some serious consideration since changes to
$compiletend to have far-reaching effects and unintended consequences.I don't have a particularly strong opinion on this.
@IgorMinar @caitp @tbosch what do you think?
caitp commentedon Feb 22, 2014
Right now, the compiler doesn't really track whether or not an expression makes a function call or not (or how many, as it could be more than one). I don't feel like this really makes a lot of sense.
If we did force an expression to do something meaningful (for some definition of "meaningful", by throwing if it doesn't), this would break some apps.
I guess it's a tradeoff between notifying people that they might be doing something they don't expect to do, but I think that given how hard it is to tell if an expression is doing something useful, it's probably not a good idea to change this.
edit: It isn't that bad to make the "optional" flag actually do something for
&properties, though. That seems okay with me.edit again: actually, making these optional would still be a breaking change for directives using optional-flagged expression properties, because it would require the directive to test for the presence of the scope property before evaluating the expression. This seems bad.
IgorMinar commentedon Feb 22, 2014
I agree that the current behavior is weird. It is not intentional.
We should fix this in 1.3
davidemoro commentedon Feb 22, 2014
Ok, how can I help you? I want to dive into angular and there are tickets that needs a PR, so I would be glad to contribute.
I just need a mentor that reviews my code
9 remaining items
fix($compile): throw when non-optional '&' binding is used but not sp…
fix($compile): throw when non-optional '&' binding is used but not sp…
fix($compile): throw when non-optional '&' binding is used but not sp…
fix($compile): throw when non-optional '&' binding is used but not sp…
fix($compile): throw when non-optional '&' binding is used but not sp…
yellow1912 commentedon Dec 16, 2014
I think there should be a way to check if the function is passed in or not, right now it's just too weird. I have to manually check if the attribute exists.
fix($compile): do not initialize optional '&' binding if attribute no…
fix($compile): do not initialize optional '&' binding if attribute no…
sramilli commentedon Sep 7, 2016
Hi,
i still see this behaviour in Angular 1.5.8
i´m passing an optional function to a sub-sub-component with "&?" in both steps.
The first sub-component detects "undefined", but the sub-sub-component got a function instead.
In my sub-component's template i would like to avoid having some logic toggling between
<sub-sub-component some-attribute="$ctrl.anAttribute">.......</...>and
<sub-sub-component some-attribute="$ctrl.anAttribute" optional-function="$ctrl.myFunction()">.....</...>As a workaround i use instead "=" as shown
bindings: { myFunction: "=" }ashclarke commentedon Sep 7, 2016
@sramilli I had the exact same issue today (1.5.8 too). Workaround I applied was the same too.
(Fun fact: I came to this thread again via Google, and then got freaked out just now because I got this notifcation. Wasn't until I visited GitHub to reply that I realised that I'd already participated in this issue over 2 years ago.)
tabanliviu commentedon Feb 27, 2017
@davidemoro @caitp I'd like to mention that we recently hit upon this issue during our upgrade from 1.3 to 1.5 and our unit tests did not capture this behavior because in 1.3 there was no difference in the outcome between using a directive without the optional attribute vs having an optional value that is undefined.
I think this should have been marked as a breaking change or at least had some note about potential breaking change.
I admit I didn't read the full change log line by line and only looked at the breaking changes section but still looking back I would have preferred some hint about this change.
tommck commentedon Sep 17, 2018
This problem still exists in 1.6.10
Optional expression is always a function