【问题标题】:Dynamically generate directive based on $watch基于$watch动态生成指令
【发布时间】:2015-10-16 17:00:41
【问题描述】:

我想根据 $watch 动态生成指令。这是我的代码,它只是记录服务对象的值:

  gasStation.directive('createMenuTree', ['customerType', function (customerType) {
    console.log(customerType.getCustomerType() + ' enterring a directive');
    var template;


    console.log(customerType.getCustomerType() + ' from directive');

    var linker = function(scope){console.log()}

    return {

        controller: ['$scope', function ($scope) {}],

        link: function(scope){
            scope.$watch(function(){
                console.log(customerType.getCustomerType() + ' watcher');
                if (customerType.getCustomerType() == 'REGULAR') {
                    template = 'dashboard/regular_dashboard.html';
                }
                if (customerType.getCustomerType() == 'BUSINESS') {
                    template = 'dashboard/business_dashboard.html';
                }
                return customerType.getCustomerType();
            });
        },

        templateUrl: template
    };
}]);

我是如何使用指令的:<create-menu-tree></create-menu-tree>

问题是:如何根据customerType.getCustomerType() 值设置templateUrl 变量?目前,template 的值未定义。

【问题讨论】:

    标签: angularjs angularjs-directive directive


    【解决方案1】:

    您可以在指令链接函数中使用$http服务根据客户类型动态获取html字符串,然后使用$compile将html字符串与您的范围绑定,最后将<create-menu-tree>内容替换为新编译的元素

    gasStation.directive('createMenuTree', ['customerType', '$compile', '$http', 
    
    function (customerType, $compile, $http) {
        return {
            restrict:'EA',
            template: '',
            scope:false,
            compile: function(){
                return {
                   pre: function(scope, element, attr){
    
                       var templateUrl = '';
    
                       if (customerType.getCustomerType() == 'REGULAR') {
                          templateUrl = 'dashboard/regular_dashboard.html';
                       }
                       if (customerType.getCustomerType() == 'BUSINESS') {
                          templateUrl  = 'dashboard/business_dashboard.html';
                       }
    
                       $http.get(templateUrl).then(function(htmlString){
                            var templateEle = $compile(htmlString)(scope);
                            element.empty();
                            element.append(tempalteEle);
                       })
    
                   },
                   post: function(scope, element, attr){}
            }
        }
    }]);
    

    【讨论】:

    • 感谢您的回复。是否可以避免使用 $http 服务?使用 ... 可能有点矫枉过正
    • 是的,您可以避免使用$http。它基于您存储模板的位置。这里我假设您将模板放在单独的 html 文件中。您可以使用$templateCache 来存储和获取模板
    • 好的,明白了。还有一个问题:我不明白下面的变量元素是什么?它来自哪里?
    • 调用link函数时,angular会在函数中注入$element服务,$element表示要使用指令的元素,检查$compile api doc
    • 好的,了解变量。同时,在尝试您的方法时出现以下错误:`TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.`
    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 2023-03-15
    • 2019-12-10
    • 1970-01-01
    • 2018-02-13
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多