-
Notifications
You must be signed in to change notification settings - Fork 27.5k
directives that have '=' binding, changing scope value causes error if not specified(optional) #1435
Comments
I would like to see any easy way for optional arguments / argument defaults on directive attributes as well. |
Created a jsFiddle to expose the same issue with the latest version of AngularJS: http://jsfiddle.net/yA2rn/ |
Just chiming in that this is something that would be very useful to me as well. thanks |
ditto. |
+1 |
I also think this would be very useful. Right now I am doing this in my directives: var optionalParameters = scope.$eval(attributes.optionalParameters); which works ok if I don't need bi-directional binding with the parent scope. |
+1 |
If you bind using '=' to a non-existant parent property, the compiler will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception, which is right because the model doesn't exist. This enhancement allow to specify that a binding is optional so it won't complain if the parent property is not defined. In order to mantain backward compability, the new behaviour must be specified using '=?' instead of '='. The local property will be undefined is these cases. Closes angular#909 Closes angular#1435
That's good. Now, I make optional binding like this: link: function(scope, element, attrs) {
if (attrs.selected) {
getSelected = $parse(attrs.selected);
setSelected = getSelected.assign;
scope.$watch(
function watchSelected() {
return getSelected(scope.$parent);
},
function updateSelected(value) {
if(setSelected) {
setSelected(scope.$parent, value);
}
scope.selected = value;
}
);
scope.selected = getSelected ? getSelected(scope.$parent) : false;
}
} This code can be replaced by simple config scope: {
selected: "=?"
} |
Please merge lrlopez's fix with the master build! |
+1 |
3 similar comments
+1 |
+1 |
+1 |
I'm seeing this issue in 1.0.7 and getting an error when using =? Error: Is this issue back!?? |
This feature was merged into the 1.1.x branch so it's not available in 1.0.x, sorry... |
Oh ok, thank you. On Aug 29, 2013, at 5:40 PM, Luis Ramón López [email protected] wrote:
|
Understanding that it works to use the syntax Could they always be optional? This makes the API simpler. Is there a performance issue there? |
@SimpleAsCouldBe conversely, performance would be improved if the required binding will be removed, because regexp will become simpler. |
Someone from @angular, please take attention for this question |
We might want to make this a separate feature request, @just-boris. Succinctly, the request is: "Make Wondering about performance and structure implications. Also maybe some folks depend on the enforcement of this. I tend to think the better default is "just work/silently fail", sort of like exceptions in templates ( |
Thanks |
The above was quoted from the $compile documentation. I see it's been mentioned before, but after that there are more people calling for this feature who might only read the first and last post. |
it won't throw if the parent scope property doesn't exist --- it will throw if the expression is just not assignable (for instance, |
@bernhard-hofmann Thanks for the explanation, this fixed it for me. To improve the documentation, you should know that I think the users are confused (I was at least) because many guides including the official one (https://docs.angularjs.org/guide/directive) mention '&', '@', and '=' syntax... (which is already confusing)... no guides that I read mention '=?' syntax. Nothing would have prompted me to look into the documentation for $compile since I'm not touching that service with my code. |
Agree with @joshribakoff , I would never found this in the docs unless someone from my team pointed me to this thread. |
@just-boris I can't use angular 1.1.x so I'm using your approach. The problem is that I can't make a two ways binding with your approcah meaning that when I change the scope variable in the directive it doesn't get updated in the container controller. Would you clarify a little bit your solution ? |
@LeonardoGentile updated my code snippet, now it has a call of |
Thanks @just-boris but that did not work for me...I had to make a little modification:
|
+1 |
@marsechelon I don't know why you are +1 this as optional bindings have been in Angular since 1.1.x. |
If I have a directive with a scope definition, and setting a value in the scope:
And create it like
With optionalValue not specified, it yields the NON_ASSIGNABLE_MODEL_EXPRESSION error.
But I'd expect it to work in this case.
This could be solved in the "scope mode parser" by simply not applying the binding:
Or is this intentionally? If so, maybe add a new symbol like '~' for optional attributes?
(just realized that I opened essentially the same issue 3 months ago: #1131)
The text was updated successfully, but these errors were encountered: