javascript - AngularJs 创建模板指令

标签 javascript angularjs templates

我想创建包含更多模板的指令。 所选模板取决于某些值(如模板类型)。 然后,如果我在 html 页面中调用我的指令,并更改类型模板类型,则需要更改 html 模板。 像这样:

<template-factory template-type={{foo}}></template-factory>

我认为我可以创建一个包含我所有模板的 html 模板,并从 ng-if 帮助中选择。但是我认为它不是很好。 请帮助我,为此任务选择最佳解决方案。

最佳答案

有趣的是,您可以在指令中传递一个函数作为您的模板,然后该函数可以返回一个用于模板的字符串。

看看What are the benefits of a directive template function in Angularjs?了解这是如何完成的以及优缺点。

来自angular docs :

angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', function($scope) {
  $scope.customer = {
    name: 'Naomi',
    address: '1600 Amphitheatre'
  };
}])
.directive('myCustomer', function() {
  return {
    templateUrl: function(elem, attr){
      return 'customer-'+attr.type+'.html';
    }
  };
});

关于javascript - AngularJs 创建模板指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37157708/

相关文章:

c++ - 包含命名空间的类模板的前向声明导致编译错误

templates - 获取 Sitecore 模板 A 或模板 B 下的所有子项目

javascript - 具体组合算法

javascript - 如何使用 DataTable 获取行值并删除数据?

javascript - 如何将边框半径应用于具有水平滚动的分隔表行?

javascript - jQuery icheck 插件在追加后不起作用

javascript - 使用 angularjs 1.4 提交验证不起作用

javascript - AngularJS 智能表全局配置实现

javascript - jQuery 悬停功能不适用于 ng-repeat Angular 对象

c++ - Lambda 作为模板参数