【问题标题】:AngularJs create templates directiveAngularJs 创建模板指令
【发布时间】:2016-05-11 08:59:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript angularjs templates


    【解决方案1】:

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

    查看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';
        }
      };
    });
    

    【讨论】:

    • 谢谢乔伊。你对我的任务很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2015-03-17
    • 1970-01-01
    相关资源
    最近更新 更多