directives that have '=' binding, changing scope value causes error if not specified(optional)
If I have a directive with a scope definition, and setting a value in the scope:
directive('some', function () {
return {
scope: {optionalValue: '='},
bind: function(scope) {
scope.optionalValue = something;
}
}
}
And create it like
<some />
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:
case '=': {
if (attrs[attrName] == null) {
return;
}
...
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: https://github.com/angular/angular.js/issues/1131)
Thanks
If the parent scope property doesn't exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception. You can avoid this behavior using =? or =?attr in order to flag the property as optional.
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, foo.bar is assignable, while foo.bar() is not) --- need an lvalue
@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 ? It's not very clear, I'm missing something..
@LeonardoGentile updated my code snippet, now it has a call of setSelected(scope.$parent, value);
But there is already three major releases, so I advice you to update
Thanks @just-boris but that did not work for me...I had to make a little modification:
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) {
scope.selected = value
}
);
scope.selected = getSelected ? getSelected(scope.$parent) : false;
}
scope.$watch('selected', function(value) {
if(setSelected) {
setSelected(scope.$parent, value)
}
})
}
+1
@marsechelon I don't know why you are +1 this as optional bindings have been in Angular since 1.1.x.