javascript - 使用 ng-click 更改指令模板

标签 javascript angularjs angular-directive

我在 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/

相关文章:

javascript - 添加 Angularjs 代码后,我无法在 Chrome 中右键单击网页

angularjs - Angular JS : Controller not working

angular - 检查组件上是否存在输出

javascript - AngularJS : Scope inaccessible in directive after minification

javascript - 创建后将 onchange 事件添加到 javascript 小部件

javascript - asp.net 如何自动启用浏览器位置设置

javascript - 单击“提交”后客户端表单验证

javascript - jQuery UI sortable 的 serialize 方法排除了一项

javascript - ng-inspector 会减慢并导致页面崩溃。在哪里看

javascript - 在结构指令中访问 ViewChildren/ContentChildren