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)
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/ The use case seems to be a valid one...
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
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
+1
+1
+1
I'm seeing this issue in 1.0.7 and getting an error when using =?
Error: Invalid isolate scope definition for directive ...: =?
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:
This feature was merged into the 1.1.x branch so it's not available in 1.0.x, sorry...
— Reply to this email directly or view it on GitHub.
Understanding that it works to use the syntax = for a required 2-way binding and =? for an optional 2-way binding, I wonder why both are needed.
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. So I agree with you.
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 = an optional binding all the time"
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 ({{ myUndefinedObject.name }}) don't throw. If enforcement is wanted syntax like =! or === could opt-in.