1

How can I read a directive value like the info attribute in the scope?

<div ng-controller="Controller">
  <my-customer info="naomi"></my-customer>
  <hr>
  <my-customer info="igor"></my-customer>
</div>

For example with this controller:

angular.module('docsIsolateScopeDirective', [])
  .controller('Controller', ['$scope', function($scope) {
    $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
    $scope.igor = { name: 'Igor', address: '123 Somewhere' };

    //can I read the info value?
  }])
  .directive('myCustomer', function() {
    return {
      restrict: 'E',
      scope: {
        customerInfo: '=info'
      },
      templateUrl: 'my-customer-iso.html'
    };
  });

You can find it on plunker, or similar in the angular documentation.

2 Answers 2

1

To associate a controller with a directive, you can use the Directive Definition Object's controller property. Either specifying the controller as a function or specifying the name of the controller.

Angular App

angular.module('docsIsolateScopeDirective', [])
  .controller('Controller', ['$scope', '$attrs', function($scope, $attrs) {
    $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
    $scope.igor = { name: 'Igor', address: '123 Somewhere' };
    //Here are the attrs values
    console.log($attrs.stars);
    console.log($attrs.info);
  }])
  .directive('myCustomer', function() {
    return {
      restrict: 'E',
      scope: {
            starts: '=stars',
            info: '=info'
      },
      //Associate a controller
      controller: 'Controller',
      template: "Name: {{customerInfo.name}} Address: {{customerInfo.address}}"
    };
  });

HTML

<div ng-controller="Controller">
  <my-customer info="naomi" stars="star1"></my-customer>
  <hr>
  <my-customer info="igor" stars="star2"></my-customer>
</div>

(function(angular) {
  'use strict';
angular.module('docsIsolateScopeDirective', [])
  .controller('Controller', ['$scope', '$attrs', function($scope, $attrs) {
    $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
    $scope.igor = { name: 'Igor', address: '123 Somewhere' };
    //Here are the attr values
    console.log($attrs.stars);
    console.log($attrs.info);
  }])
  .directive('myCustomer', function() {
    return {
      restrict: 'E',
      scope: {
            starts: '=stars',
            customerInfo: '=info'
      },
      //Associate controller
      controller: 'Controller',
      template: "Name: {{customerInfo.name}} Address: {{customerInfo.address}}"
    };
  });
})(window.angular);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-directive-isolate-production</title>
</head>
<body ng-app="docsIsolateScopeDirective">
  <div ng-controller="Controller">
  <my-customer info="naomi" stars="star1"></my-customer>
  <hr>
  <my-customer info="igor" stars="star2"></my-customer>
</div>
</body>
</html>

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

8 Comments

Have you tested it? I get the errors for your first line "Unexpected token function" and "Uncaught Error: [$injector:modulerr]".
@NewDeveloper Wait for few minutes, let me give you full example
@NewDeveloper check now, you can see in the console.
$scope.info is already used, so i changed it to $scope.attrsInfo = $attrs.info. You can see it on plunker. But the value of $scope.attrsInfo in the controller is undefinded.
@NewDeveloper check my answer, I've updated, it's working now, just replace the code.
|
0

As its name suggests, isolated scopes are isolated, and that's why you can use 2-way binding - like you're doing with customerInfo: '=info'.

When you make changes inside the myCustomer directive scope, it will bubble up in the scope hierarchy and will change the users object in your controller:

angular.module('docsIsolateScopeDirective', [])
   .controller('Controller', ['$scope', function($scope) {
       $scope.users = [{
           first_name: 'naomi',
           last_name: 'doe'
       },
       {
           first_name: 'igor',
           last_name: 'doe'
       }]
}])

And your template should look like this:

<div ng-controller="Controller" ng-repeat="user in users">
    <my-customer info="user"></my-customer>
    <hr>
</div>

Comments

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.