Pass a value from parent directive template to child directive

Dan Beaulieu

Problem:

I'm attempting to pass a value from an ng-repeat into a child-directive but when I try to access my passed variable in directive 2 I get "undefined".

Here's an illustration of what I am attempting. Basically directive 1 represents an array of widgets while directive 2 represents a single widget. I am attempting to pass an item from the ng-repeat loop into my child directive.

enter image description here

My Attempt:

Here's a simplified version of my directive 1 template:

<li ng-repeat="item in widgets">
    <directive2 item="item"></directive2>
</li>

Here's a simplified version of directive 2:

angular.module('directive2').directive(
    ['$compile', '$rootScope',
    function ($compile, $rootScope) {
        return {
        restrict: 'E',
            scope: { item: '=' },
            templateUrl: 'ext-modules/tile/widgetTemplate.html',
            link: function (scope, element, attrs) { 
                console.log(scope.item); // undefined
            }
        };
    }]);

The ng-repeat on widgets creates two items and I have verified that the data exists. The application works fine and doesn't throw an error but my console.log returns : undefined.

My Question:

How can I pass a value from a directive template's ng-repeat into a child-directive?


here's a fiddle: http://jsfiddle.net/3znEu/112/

masa

Yet another solution proposal:

HTML:

<div ng-controller="MyCtrl">
  <directive1></directive1>
</div>

JavaScript:

angular.module('myApp', [])
  .controller('MyCtrl', ['$scope', function ($scope) {
    $scope.widgets = [
      'a', 'b', 'c', 'd'
    ];
   }])
.directive('directive1', function () {
  return {
    restrict: 'E',
    scope: false,
    template: 
      '<li ng-repeat="item in widgets">' +
        '<directive2 item="item"></directive2>' +
      '</li>'
  }
})
.directive('directive2', ['$compile', '$rootScope',
  function ($compile, $rootScope) {
    return {
      restrict: 'E',
      scope: { item: '=' },
      template: 
        '<div>elem = {{item}}</div>',
      link: function (scope, element, attrs) { 
        console.log(scope.item);
      }
   }
}]);

Fiddle: http://jsfiddle.net/masa671/dfn75sp3/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

From Dev

Pass variable from parent directive to child-directive?

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Calling a function in a parent directive from child directive

From Dev

AngularJS: Requiring a parent directive from child directive

From Dev

AngularJS: Requiring a parent directive from child directive

From Dev

AngularJS : directive binding value to parent scope from template model

From Dev

AngularJS : directive binding value to parent scope from template model

From Dev

How to pass a value from an AngularJS directive to a function in parent controller

From Dev

Pass value from controller to directive

From Dev

Pass a value from a controller to directive

From Dev

Why can't I require a child directive from a parent directive?

From Dev

AngularJS : Child directive scope to inherit from parent directive isolated scope?

From Dev

Angular js passing a variable from a parent directive to a child directive

From Dev

Angular: invoke method in child directive from parent directive

From Dev

Calling a function from a child directive & using a variable in the parent directive

From Dev

Defer compilation or linking of a child directive from the parent

From Dev

Passing method from parent to child directive

From Dev

Defer compilation or linking of a child directive from the parent

From Dev

Accessing parent "isolated" scope from child directive

From Dev

Update the Parent Scope from Child Directive

From Dev

Get parent directive in child

From Dev

Angular: get Template URL from parent directive

From Dev

pass function from parent controller to directive with parameter

From Dev

Angular Directive parent template?

From Dev

How to Pass data from directive template to controller?

From Dev

AngularJS pass variable from custom directive to template

From Dev

How to pass scope from directive to template

Related Related

  1. 1

    Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

  2. 2

    Pass variable from parent directive to child-directive?

  3. 3

    Use, in child directive, value from parent directive and $scope

  4. 4

    Use, in child directive, value from parent directive and $scope

  5. 5

    Calling a function in a parent directive from child directive

  6. 6

    AngularJS: Requiring a parent directive from child directive

  7. 7

    AngularJS: Requiring a parent directive from child directive

  8. 8

    AngularJS : directive binding value to parent scope from template model

  9. 9

    AngularJS : directive binding value to parent scope from template model

  10. 10

    How to pass a value from an AngularJS directive to a function in parent controller

  11. 11

    Pass value from controller to directive

  12. 12

    Pass a value from a controller to directive

  13. 13

    Why can't I require a child directive from a parent directive?

  14. 14

    AngularJS : Child directive scope to inherit from parent directive isolated scope?

  15. 15

    Angular js passing a variable from a parent directive to a child directive

  16. 16

    Angular: invoke method in child directive from parent directive

  17. 17

    Calling a function from a child directive & using a variable in the parent directive

  18. 18

    Defer compilation or linking of a child directive from the parent

  19. 19

    Passing method from parent to child directive

  20. 20

    Defer compilation or linking of a child directive from the parent

  21. 21

    Accessing parent "isolated" scope from child directive

  22. 22

    Update the Parent Scope from Child Directive

  23. 23

    Get parent directive in child

  24. 24

    Angular: get Template URL from parent directive

  25. 25

    pass function from parent controller to directive with parameter

  26. 26

    Angular Directive parent template?

  27. 27

    How to Pass data from directive template to controller?

  28. 28

    AngularJS pass variable from custom directive to template

  29. 29

    How to pass scope from directive to template

HotTag

Archive