在指令属性设置templateUrl基地 [英] Set templateUrl base on attribute in directive

查看:103
本文介绍了在指令属性设置templateUrl基地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一组角指令,我想根据属性的presence或价值来加载正确的模板。

I'm working on a set of angular directives and I want to load the correct template based on the presence or value of an attribute.

<my-form horizontal> </my-form>
<my-form vertical> </my-form>

如果水平,templateUrl应 /谐音/水平形式
如果垂直,templateUrl应 /谐音/垂直形式

If horizontal, templateUrl should be /partials/horizontal-form and If vertical, templateUrl should be /partials/vertical-form

我感兴趣的templateUrl,因为我不能使用模板由于HTML取决于属性。在编译。pre 功能,HTML已经被载入。

I'm interested in the templateUrl because I can't use template since the html depends on the attributes. In the compile.pre function, the html has already been loaded.

如果有实现这一目标的另一种方式,我接受它,因为我现在的角度出发和更多信息的更好的。

If there is another way of achieving this, I'm open to it since I'm now starting with angular and the more info the better.

感谢

推荐答案

这方面一个解决方案是使用NG-包括模板文件中。

One of the solutions for this is to use an ng-include inside the template file.

在模板文件中的第一个属性将有类似:

The first attribute inside of the template file will have something like :

<div ng-include = "getTemplate()">

</div>

在您的指令,code,你会写是这样的:

In your directive code you would write something like :

scope : {direction : "="},
link : function(scope,element,attrs)
{
    scope.getTemplate = function(){
        if(scope.direction === "horizontal")
        {
            return "horizontal.html";
        }
        return "vertical.html";
    }
}

希望这有助于!

这篇关于在指令属性设置templateUrl基地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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