AngularJS将变量传递给模板

时间:2014-11-19 17:16:21

标签: javascript jquery html angularjs

我想将变量或文本传递给模板,以便在模板中显示值。

我遇到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>

1 个答案:

答案 0 :(得分:6)

http://jsfiddle.net/f97keutL/

<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"; 
});

您只需在范围中设置变量,并包含模板?