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

Thanks

wbeange avatar Aug 19 '14 21:08 wbeange

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.

bernhard-hofmann avatar Sep 23 '14 14:09 bernhard-hofmann

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

caitp avatar Sep 23 '14 14:09 caitp

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

joshribakoff avatar Oct 03 '14 23:10 joshribakoff

Agree with @joshribakoff , I would never found this in the docs unless someone from my team pointed me to this thread.

BorisKozo avatar Nov 26 '14 07:11 BorisKozo

@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 avatar Dec 16 '14 15:12 LeonardoGentile

@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

just-boris avatar Dec 16 '14 18:12 just-boris

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)
        }
    })   
}

LeonardoGentile avatar Dec 19 '14 11:12 LeonardoGentile

+1

spike442 avatar Aug 28 '16 16:08 spike442

@marsechelon I don't know why you are +1 this as optional bindings have been in Angular since 1.1.x.

Narretz avatar Aug 29 '16 12:08 Narretz