角指令不同的模板 [英] Angular Directive Different Template

查看:129
本文介绍了角指令不同的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量类型指令myDirective。如果我运行<我-指令类型=X> 我要的指令以使用templateUrl:X-template.html。
如果我做<我-指令型=Y> 我要的指令以使用templateUrl:Y-template.html。

I have a directive myDirective with variable type. If I run <my-directive type="X"> I want the directive to use templateUrl: x-template.html. If I do <my-directive type="Y"> I want the directive to use templateUrl: y-template.html.

这是我目前的指令。

app.directive('myDirective', function() {
    var myDirective = {
        templateUrl: 'X-template.html',
        restrict: 'E',
        scope: {
            type: '=' 
        },
    };
    return myDirective;
});

我读直通计算器和角文档,但没有发现任何东西,我需要的。

I read thru stackoverflow and angular documentation but have not found anything that I need.

我现在正在努力做的线沿线的东西:

I am now trying to do something along the lines of:

if ($scope.type === 'X') {
    templateUrl: 'X-template.html',
}
else if ($scope.type === 'Y') {
    templateUrl: 'Y-template.html',
}

但不知道在哪里做。

But do not know where to do it.

你们是否知道这是可能的,怎么了?

Do you guys know if this is possible and how?

推荐答案

您可以解决这个问题使用 NG-包括编译

app.directive('myDirective', function() {
    return {
        restrict: 'E',
        compile: function(element, attrs) {
            element.append('<div ng-include="\'' + attrs.type + '-template.html\'"></div>');
        }
    }
});

小提琴

这篇关于角指令不同的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆