最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS pass variable through to a template - Stack Overflow

programmeradmin5浏览0评论

I want to pass a variable or text through to a template so that it shows the value inside my template.

I e across jsFiddle which shows it working but it uses ng-repeat. Is there a simplere way to do this without using 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>

I want to pass a variable or text through to a template so that it shows the value inside my template.

I e across jsFiddle which shows it working but it uses ng-repeat. Is there a simplere way to do this without using 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>
Share Improve this question asked Nov 19, 2014 at 17:16 ngplaygroundngplayground 21.7k37 gold badges98 silver badges174 bronze badges 2
  • Using just one div?? <div ng-repeat="name in ['John','Jack']" ng-include="'partial.html'"></div> – Hackerman Commented Nov 19, 2014 at 17:19
  • The above divs would be split to other parts of a page so no need for ng-repeat – ngplayground Commented Nov 19, 2014 at 17:20
Add a ment  | 

1 Answer 1

Reset to default 7

http://jsfiddle/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"; 
});

You just set the variable in the scope, and include the template?

发布评论

评论列表(0)

  1. 暂无评论