【发布时间】:2014-01-03 19:24:22
【问题描述】:
我正在为 Bootstrap 使用 AngularJs-UI 组件。我想将填写好的模板插入到弹出功能的数据元素之一中。这适用于所有不在 ng-repeat 内的元素。如何让 ng-repeat 元素在插值模板中工作?
我在 http://plnkr.co/edit/Cuku7qaUTL1lxRkafKzv 有一个 plunker,它不工作,因为我不知道如何在 plunker 中获取 Angular-UI-bootstrap。
<div data-popover="{{createHTML()}}">some content</div>
我的本地作用域有函数createHTML(),看起来像这样。
angular.module('myApp', ['ngSanitize'])
.controller("myController", function(myService){
$scope.createHTML = function() {
var thingy = { blah: "foobar", collection: [ "1", "2", "3" ] };
return myService.html_for(thingy);
}
});
服务是
angular.module('myApp')
.service('myService', function($templateCache, $interpolate, $sanitize, $log) {
"use strict";
function html_for(thingy) {
var template = $templateCache.get('thingyTemplate.html'),
link = $interpolate(template),
html = link(thingy),
unsafe = $sanitize(html);
return unsafe;
}
return {
html_for: html_for
}
});
模板:
<script type="text/ng-template" id="thingyTemplate.html">
<div>
<div><strong>Blah:</strong> {{blah}}</div>
<div data-ng-repeat="foo in collection"><strong>Collection:</strong> {{foo}}</div>
<div><strong>Collection[0]:</strong> {{collection[0]}}</div>
<div><strong>Collection[1]:</strong> {{collection[1]}}</div>
<div><strong>Collection[2]:</strong> {{collection[2]}}</div>
</div>
</script>
<script type="text/ng-template" id="template/popover/popover.html">
<div class="popover {{placement}}" data-ng-class="{ in: isOpen(), fade: animation() }">
<div class="arrow"></div>
<div class="popover-inner">
<h3 class="popover-title" data-ng-bind="title" data-ng-show="title"></h3>
<div class="popover-content" data-ng-bind-html="content"></div>
</div>
</div>
</script>
【问题讨论】:
标签: javascript angularjs twitter-bootstrap