角度 - 单击时更新指令模板 [英] angular - update directive template on click
问题描述
我正在尝试使用 Angular 构建页面.
I am trying to build a page using Angular.
我有两个模板,比如说
<script type='text/ng-template' id='a.html'>
/*some html using {{data}} */
<div><button>A</button></div>
</script>
和
<script type='text/ng-template' id='b.html'>
/*some html using {{data}} */
<div><button>B</button></div>
</script>
我想创建一个指令,当单击模板内的按钮时,将其模板从 a 更改为 b,反之亦然.我尝试了几种选择,查看类似问题的其他答案,但没有任何运气.你能帮我找到最简单的方法吗?
I would like to create a directive that changes its template from a to b and vice versa when the button inside the template gets clicked. I tried several options, looking at other answers to similar questions, but without any luck. Can you help me to find the easiest way to do this?
推荐答案
ng-include
是一个漂亮的指令,它接受一个范围变量作为参数.这样,您就可以动态加载(包括加载模板中的其他指令).请参阅 plnkr http://plnkr.co/edit/qxBVDWx6P0F0aNuOymct?p=preview
ng-include
is a nifty directive that accepts a scope variable as the parameter. this way, you can dynamically load (including other directives from the loaded templates). See the plnkr http://plnkr.co/edit/qxBVDWx6P0F0aNuOymct?p=preview
app.directive('dynamicTemplate', function(){
return {
restrict: 'A',
replace: true,
template: '<div><div ng-include="var"></div><div>current var: {{ var }}</div></div>',
controller: function($scope){
$scope.var = 'template1.html';
$scope.change = function(where){
$scope.var = where;
}
}
};
});
但最好的方法是使用 $stateProvider
或 $routeProvider
but the best way would be just deal with either using $stateProvider
or $routeProvider
这篇关于角度 - 单击时更新指令模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!