在指令属性设置templateUrl基地指令、属性、基地、templateUrl

2023-09-13 02:54:15 作者:孤久成瘾

我工作的一组角指令,我想根据属性的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";
    }
}

希望这有助于!