angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

directives that have '=' binding, changing scope value causes error if not specified(optional)

Open johnnyelwailer opened this issue 14 years ago • 30 comments

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)

johnnyelwailer avatar Oct 03 '12 07:10 johnnyelwailer

I would like to see any easy way for optional arguments / argument defaults on directive attributes as well.

NickHeiner avatar Oct 25 '12 16:10 NickHeiner

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...

pkozlowski-opensource avatar Nov 24 '12 23:11 pkozlowski-opensource

Just chiming in that this is something that would be very useful to me as well. thanks

presstube avatar Dec 06 '12 18:12 presstube

ditto.

bendalton avatar Dec 06 '12 19:12 bendalton

+1

fx avatar Dec 13 '12 02:12 fx

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.

ryanzec avatar Dec 24 '12 14:12 ryanzec

+1

tschieggm avatar Jan 26 '13 06:01 tschieggm

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: "=?"
}

just-boris avatar Jan 28 '13 06:01 just-boris

Please merge lrlopez's fix with the master build!

roypeled avatar Feb 03 '13 07:02 roypeled

+1

Mischi avatar Feb 14 '13 14:02 Mischi

+1

aaronwhite avatar Feb 16 '13 16:02 aaronwhite

+1

innovationhero avatar Feb 17 '13 00:02 innovationhero

+1

marcospassos avatar Feb 25 '13 20:02 marcospassos

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!??

rgraffconnect avatar Aug 29 '13 19:08 rgraffconnect

This feature was merged into the 1.1.x branch so it's not available in 1.0.x, sorry...

lrlopez avatar Aug 29 '13 21:08 lrlopez

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.

rgraffconnect avatar Aug 29 '13 21:08 rgraffconnect

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?

SimplGy avatar Mar 12 '14 16:03 SimplGy

@SimpleAsCouldBe conversely, performance would be improved if the required binding will be removed, because regexp will become simpler. So I agree with you.

just-boris avatar Mar 12 '14 18:03 just-boris

Someone from @angular, please take attention for this question

just-boris avatar Mar 15 '14 12:03 just-boris

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.

SimplGy avatar Jul 22 '14 17:07 SimplGy