Monday, 15 August 2011

javascript - How can i use scope's variable in directive? -



javascript - How can i use scope's variable in directive? -

i want scope's variable in directive javascript variable. here code:

app.controller("home", ["$scope", function($scope) { ... $scope.nb_msg = data.length; ... }]); app.directive("mydiv", function() { // here, want $scope.nb_msg var nb_msg = ???; var result = []; // nb_msg result var template = ""; ... for(var i=0; i<10; i++) { template += "<span>" + result[i] + "</span>"; } ... homecoming { restrict: "e", template: template }; });

how can do? in advance!

you can access scope in link function:

app.directive("mydiv", function() { homecoming { restrict: "e", template: '<span ng-repeat="i in result">{{i}}</span>', link: function(scope, element, attr) { // here, want $scope.nb_msg var nb_msg = scope.nb_msg; scope.result = []; for(var i=0; i<10; i++) { scope.result.push(i); } } }; });

javascript angularjs scope angularjs-directive

No comments:

Post a Comment