我想将变量或文本传递给模板,以便在模板中显示值。
我遇到jsFiddle,显示它正常工作,但它使用ng-repeat
。是否有一种简单的方法可以在不使用ng-repeat
的情况下执行此操作?
<div ng-repeat="name in ['John']" ng-include="'partial.html'"></div>
<div ng-repeat="name in ['Jack']" ng-include="'partial.html'"></div>
<script type="text/ng-template" id="partial.html">
<div>The name is {{ name }}</div>
</script>
答案 0 :(得分:6)
<div ng-controller="ctrl">
<div ng-include="'partial.html'"></div>
</div>
<script type="text/ng-template" id="partial.html">
<div>The name is {{ name }}</div>
</script>
JS:
var myApp = angular.module('myApp',[]);
myApp.controller('ctrl', function($scope) {
$scope.name = "John";
});
您只需在范围中设置变量,并包含模板?