2

So I know when defining scope in a directive, '@' means string and '=' means two-way binding. What does '&' mean?

Here would be an example directive:

angular.module('NoteWrangler')
.directive('nwCard', function() {
  return {
    restrict: 'E',
    templateUrl: './templates/directives/nw-card.html',
    scope: {
      header: '@',
      description: '=',
      tweeted: '='
    },
    link: function(scope, element, attrs){
      if(scope.tweeted)
      element.addClass('tweeted');
    }
  };
});
1

2 Answers 2

4

So the &, @, = define how the relationships work together for how the scope isolation should work

@: text representation

=: two-way binding => allows you to manipulate the data

&: is an the manipulation of the parent scope with a value being passed through. Its typically used to pass a parent scope function through to a directive.

& is very difficult to explain in text but this link here walked me through it: https://egghead.io/lessons/angularjs-isolate-scope-expression-binding

Sign up to request clarification or add additional context in comments.

Comments

2

Simply speaking, & is to pass in a function or handler to a directive. A good start point is the AngularJS Developer Guide.

Here is a very basic example (JSFiddle):

angular.module('Joy', [])
    .controller('MyCtrl', ['$scope', function ($scope) {
        $scope.addition = function (v1, v2) {
            console.log(v1, v2);
            return v1 + v2;
        };
}])
    .directive('passMeContrller', [function () {
    return {
        restrict: 'A',
        scope: {
            add: '&',
        },
        template: '<div>{{ add({v1: 2, v2: 4}) }}</div>'
    };
}]);

HTML is:

<div ng-app="Joy" ng-controller="MyCtrl">
    <div pass-me-contrller add="addition(v1, v2)"></div>
    <hr>
</div>

Please noted that in the directive template, the function parameters should be {v1: 2, v2: 4}. It is an object, which will be decoded by Angular and passed to the controllers addition function.

For your reference: Pass callback function to directive

5 Comments

I'm a little confused by '{v1: 2, v2: 4}'. Is this just to set a default value of the output? When I do "template: '<div>{{ add() }}</div>'" the directive seems to run fine if I go "<div pass-me-contrller add="addition(1, 2)"></div>" with real numbers
Yes, you are right. Here {v1: 2, v2: 4} corresponds to the two arguments of the function addition. Of course addition(1, 2) runs well, but in this case there is no interaction between the directive and the outside controller. The example here shows that you can pass in parameters inside the directive.
Ah ok. Thank you very much for the clarification. So in my directive I could have a separate controller, that calculates something and adds it to the directives isolate scope, and then pass that to the function that was passed in by the parent. Here is an example for others: jsfiddle.net/9L07epkv/5
What does it mean if you do something like "scope: {add: '&add'}"? Doing '&add' forces that the value passed in be a function named 'add'? I saw text being put after the scope character in an example that I haven't seen before...
Check docs.angularjs.org/guide/directive. Say, if you use &myAdd, the HTML should become <div pass-me-controller my-add="addition(v1, v2)"></div>: jsfiddle.net/9L07epkv/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.