我在 Controller 中有一些带有 ng-click
的按钮,我想在单击按钮时更改指令模板。实现这一目标的最佳方法是什么? (稍后我将使用幻灯片效果的元素)
最佳答案
指令:
myModule.directive('foo', function() {
return {
scope: {
templateType: '@'
},
template: '<div ng-if="templateType == \'a\'">' +
' this is template A ' +
'</div>' +
'<div ng-if="templateType == \'b\'">' +
' this is template B ' +
'</div>'
};
});
模板:
<foo template-type="{{ templateType }}"></foo>
<a ng-click="templateType = 'b'" href="">Set template to b</a>
Controller :
$scope.templateType = 'a';
关于javascript - 使用 ng-click 更改指令模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140042/