javascript - Angular pass dynamic scope to directive -
i have directive takes attribute value http call so:
controller
app.controller("homecontroller", function($scope){ $http.get("/api/source").then(function(res){ $scope.info = res.data }); });
html
<div ng-controller="maincontroller"> <ng-mydirective data-info="{{info}}"></ng-mydirective> </div>
directive:
app.directive("ngmydirective", function(){ return { restrict: "e", templateurl: "/partials/info.html", link: function(scope, element, attrs){ scope.info = attrs.info; } } });
i'm trying pass info variable controller, directive, through attribute. doesn't work because variable value created asynchronous http call, @ time directive created, there no value.
is there way this?
you can observe attribute's value:
link: function(scope, element, attrs){ attrs.$observe('info', function(val) { scope.info = val; }) }
also, if can change directive scope isolated, can binding values:
app.directive("ngmydirective", function(){ return { restrict: "e", scope: {info: '='}, templateurl: "/partials/info.html", link: function(scope, element, attrs){ } } }); <div ng-controller="maincontroller"> <ng-mydirective info="info"></ng-mydirective> </div>
Comments
Post a Comment