1

In my directive I created an isolated scope binding an expression to an attribute:

scope: {
    foo: '@'
}

And in the directive if I try to console log scope.foo, I will get the following function:

function (locals) {
    return parentGet(scope, locals);
}

And then executing scope.foo() will invoke the expression in the parent's scope. The problem is, the expression I am passing in is an optional callback, but I have no safe way of telling if the attribute is defined from the parent. I will get the function no matter what expression the attribute holds, even if it wasn't specified.

As a result, testing for the existence of scope.foo obviously doesn't help, and I can't test for scope.foo() because that will evaluate the expression if there is one. Is there a good way to make the expression binding optional? I wish we have something similar to '=?' when binding expressions, but there doesn't seem to be a '&?'.

Xavier_Ex
  • 8,432
  • 11
  • 39
  • 55

1 Answers1

1

This is where $parse comes in handy. It delegates the tasks of determining which scope and if it's an expression or a constant. Combining this with a quick check to see if the attribute is even there you could do something like so:

.directive('myDirective', function($parse){
   return {
      link: function($scope, elem, attrs){
         if (attrs.foo){
           var fooGetter = $parse(attrs.foo)
           var callback = fooGetter($parse)
           if (callback) callback.call()
         }
      }
   }
})

Here are the docs on that - $parse docs

But my beef with this is that if you are doing any kind of controller-as implementation combined with a callback, you ofttimes get javascript scope issues when using this . I'd much prefer using the optional scope callback which is exactly why this exists, for calling methods on another scope:

scope: { foo: '&?'}

Not sure what version of Angular you're using but that is an option as I too had sought a similar solution - Optional two-way binding on isolate scope for Angular Directive

Community
  • 1
  • 1
jusopi
  • 6,791
  • 2
  • 33
  • 44
  • That's interesting, would you mind telling me which version of AngularJS that `'&?'` is available? My version is likely outdate as I don't see that being implemented in the source code, and I don't know where to search in the official docs. – Xavier_Ex Apr 29 '15 at 07:42
  • 1
    1.4.0-build.3988+sha.abf59c2 is what I'm currently using. Previously, I think when I wrote that other question, i was using 1.3. I'll see if I can find where I learned about the option callback binding. – jusopi Apr 29 '15 at 13:55
  • 1
    Here is a [**closed bug**](https://github.com/angular/angular.js/issues/6404) on the angular project, but I can't find in the docs anywhere about an optional callback binding. But you can follow the link to my original question, which links to other questions all pointing to the fact that this is possible... starting when, I'm sorry I can't say exactly. – jusopi Apr 29 '15 at 14:05