javascript - Is there a way to pass the variable to template in angularjs? -


i have angular js template called through ng-repeat create buttons.

<script type="text/ng-template" id="button.angular.template">     <div class="in-page-button" ng-if="button.mode == '' || button.mode == undefined"><< button.text >></div>     <a class="in-page-button" href="<< button.targeturl >>" target="_blank" ng-if="button.mode == 'external'"><< button.text >></a>     <a class="in-page-button" href="" ng-click="changepage(button.internallink)" target="_blank" ng-if="button.mode == 'internal'"><< button.text >></a> </script>  <!-- other code --> <div ng-repeat="(key, button) in buttons" ng-include="button.angular.temlate"> <!-- other code --> 

it works well, want use template create single button too. let's model called singlebutton. there way pass value button variable in template?

p.s. seems question not clear enough. hope use template without changing template code. great if can bind singlebutton button can directly reuse template. actual template longer 3 lines, cost time if need change template.

you can add singlebutton scope $scope.button. inside ng-repeat template use loop variable 'button', outside of use $scope.button.

here plunkr showing in action:

http://plnkr.co/edit/fkuq9vm2axo1m1ul3g8t?p=preview

index.html

<p>start repeats</p> <div ng-repeat="name in names" ng-include src="'template.html'"></div> <p>end repeats</p>  <div ng-include src="'template.html'"></div> 

app.js

app.controller('mainctrl', function($scope) {   $scope.names = ['earth', 'planet', 'bote'];   $scope.name = 'world'; }); 

template.html

<p>hello {{name}}!</p> 

Comments