javascriptangularjsangularjs-directiveisolate-scope

Use of isolate scope - @, &, and = in a simple Angular Directive


I have made a simple example with Angular.js directives for 'Celebrity Name'. I am reading about isolated scopes @,&,=, but I dont know how to use these in the following simple example to understand their usage and differences. Can someone help me out?

HTML:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
    <celebrity></celebrity> 
    <celebrity></celebrity> 
    <celebrity></celebrity> 
    <script> 
        //defining module
        var app = angular.module('myApp',[]);

        //defning Controller
        app.controller('myCtrl',function($scope){
            $scope.name = "Brad Pitt";
        });

        //defining directive
        app.directive('celebrity',function(){
            return{
                restrict: 'E',
                scope: {}, 
                template: '<div>{{name}}</div>'
            }

        });
    </script>
</body> 
</html> 

Result:

Currently all my 3 instances of directive 'celebrity' print 'Brad Pitt'. 

Please, someone tell me how to use the 3 types of isolate scope in this simple example.


Solution

  • The 3 symbols, have different use :

    html :

    <div ng-controller="myCtrl">
      <my-dir first-attr="Hello" second-attr="{{what}}"></my-dir>
    </div>
    

    js :

    angular
    .controller('myCtrl', function ( $scope ) {
      $scope.what = 'World !'
    })
    .directive('myDir', function () {
      return {
        scope : {
          firstAttr  : '@',
          secondAttr : '@'
        }
        controller : function ($scope, $log) {
          $log.log($scope.firstAttr + ' ' + $scope.secondAttr);  // logs : 'Hello World !'
        }
      };
    });
    

    html :

    <div ng-controller="myCtrl">
      <my-dir my-attr="helloWorld"></my-dir>
    </div>
    

    js :

    angular
    .controller('myCtrl', function ( $scope ) {
      $scope.helloWorld = {
        first   : 'Hello',
        second  : 'World !'
      };
    })
    .directive('myDir', function () {
      return {
        scope : {
          myAttr  : '='
        }
        controller : function ($scope, $log) {
          $scope.myAttr.second = 'Space !';
    
          $log.log($scope.myAttr.first + ' ' + $scope.myAttr.second);  // logs : 'Hello Space !'
        }
      };
    });
    

    html :

    <div ng-controller="myCtrl">
      <my-dir my-attr="helloWorld"></my-dir>
    </div>
    

    js :

    angular
    .controller('myCtrl', function ( $scope ) {
      $scope.helloWhat = function ( what ) {
        $log.log('Hello ' + what + ' !');
      };
    })
    .directive('myDir', function () {
      return {
        scope : {
          myAttr  : '&'
        }
        controller : function ($scope, $log) {
          $scope.myAttr('Angular');      // logs : 'Hello Angular !'
        }
      };
    });