比方說,我有以下標記:如何在按鈕單擊時渲染外部模板?
<main ng-controller="MainController">
<article id="story-container"></article>
<button ng-click="newSection()">+ Add Section</button>
</main>
我需要編譯和追加文章元素中下面的模板上的按鈕點擊。
<script id="newSectionTmpl" type="text/ng-template">
<section>Hello, {{name}}!</section>
</script>
我已經試過這樣的事情,但不知道這是一個好辦法:
app = angular.module('myApp', []);
app
.controller('MainController',['$scope', '$compile', function($scope, $compile) {
$scope.name = "Denis";
$scope.newSection = function() {
var tmpl = $('#newSectionTmpl').html();
var $el = $('#story-container');
console.log(tmpl);
var html = $compile(tmpl)($scope);
$el.append(tmpl);
}
}
]);
而且,我怎樣才能從外部文件加載我的模板? 這裏是鏈接到jsBin:http://jsbin.com/zuyicewexahi/5/edit?html,js,output
要加載模板外部文件,使用jquery的load()函數。這是加載外部文件非常有用的功能。欲瞭解更多信息,請查看此鏈接:http://api.jquery.com/load/ – 2014-09-04 04:35:45
謝謝!我想知道是否有角度的做法... – 2014-09-04 04:46:02
使用$ templateCache而不是jQuery。 – 2014-09-04 06:46:42