Pass the value from the parent directive template to the child directive

Problem:

I try to pass the value from ng-repeat to the child directive, but when I try to access my passed variable in directive 2, I get "undefined".

Here is an illustration of what I'm trying. Basically, directive 1 represents an array of widgets, and directive 2 represents a single widget. I am trying to pass an element from an ng-repeat loop to my child directive.

enter image description here

My attempt:

Here is a simplified version of directive 1 template :

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

Here is 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
            }
        };
    }]);

ng-repeat , , . , console.log : undefined.

:

ng-repeat child?


: http://jsfiddle.net/3znEu/112/

+4
3

:

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/

+2

, directive2 , module:

http://jsfiddle.net/3znEu/113/

      'use strict';

       var app = angular.module('myApp', [])
        .controller('myController', ['$scope', function($scope) {
            $scope.greeting = 'Hello World!';
            $scope.widgets = ["111","222","333"]
        }]);

        app.directive('directive1',
            ['$compile', '$rootScope',
            function ($compile, $rootScope) {
                return {
                restrict: 'E',
                    scope: { item: '=' },
                    template: '<div>{{item}}</div>',
                    link: function (scope, element, attrs) { 
                        console.log(scope.item); // undefined
                    }
                };
            }]);
+2

I changed your script a bit http://jsfiddle.net/3znEu/115/ .
A few changes
1. Added restriction to your directive.
2. Added a template for rendering elements (only for testing and demonstration)
3. Changed objects in the area from "@" to "="

angular.module("myApp").directive("directive1", function(){
return {
restrict: 'E',
scope: {

  item: "="
},
template: "{{item}}"
}
});
+1
source

Source: https://habr.com/ru/post/1618940/


All Articles