angularjs - Passing a scope variable to a directive -
i can see data in console.log
don't understand why can't access $scope.$parent.locations
. missing?
coupons.html
<div id="coupons-grid" coupons-repeater="locations"> </div> <!-- <p> when uncommented, below can see location data <p> <p> {{ locations }} </p> -->
directive
.directive("couponsrepeater", function() { return { compile: function(elem, attr) { return function($scope, $element, $attr) { console.log($scope.$parent); // in console see "locations" data need console.log($scope.$parent[$attr.couponsrepeater]); // comes undefined - call use because it's dynamic console.log($scope.$parent.locations); // comes undefined } } } })
couponsctrl
.controller('couponsctrl', function ($scope, $state, $http, $ionicloading, userservice) { $scope.locations = {one:1, two:2, three:3}; })
it's because $attr.couponsrepeater
undefined attr.couponsrepeater
has value looking for.
on side note, using $parent
access data not idea, directive should using isolated scope , parent should pass data via other attributes. example jsfiddle here.
app.directive("couponsrepeater", function() { return { restrict: 'a', scope: { locations: '=couponsrepeater' }, link:function (scope, ele, attrs) { console.log(scope.locations); } } });
with html.
<div coupons-repeater="locations"> </div>
Comments
Post a Comment