且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过自定义 Angular 指令有条件地应用模板?

更新时间:2022-03-25 22:48:13

您或许可以使用 template function.根据docs:

You might be able to use a template function. According to the docs:

您可以将模板指定为表示模板的字符串,也可以指定为带有两个参数 tElement 和 tAttrs(在下面的编译函数 api 中描述)并返回表示模板的字符串值的函数.

You can specify template as a string representing the template or as a function which takes two arguments tElement and tAttrs (described in the compile function api below) and returns a string value representing the template.

function resolveTemplate(tElement, tAttrs) {

}

angular.module('MyApp').directive('maybeLink', function() {
  return {
    //...
    template: resolveTemplate,
    //...
  }; 
});