ng中使用的指令包括dond'不起作用

Directives that is used in ng-include doesn't work

本文关键字:dond 不起作用 包括 指令 ng      更新时间:2023-09-26

我有如下自定义指令。

var fancySelectDirective = pluginDirecitvesModule.directive("custom-select",function(){
         return { 
           restrict: 'C',
            link: function (scope, element, attrs) {
                  element.fancySelect();
            }
        }
     });

此指令用于模板中。当我用ng-include包含这个模板时,指令不起作用,即链接函数不被调用(我试图在控制台中调试)。但当我直接在页面中使用此模板时,该指令有效。我不明白为什么会出现这个问题。

我使用ng包括以下内容:

<div id="main_wrapper" ng-include="template.html"></div>

该指令在模板中使用如下:

<select class="custom-select">

这只是一个拼写错误customSelect而不是custom-select

指令应被宣布为骆驼案;大写字母将替换为-+小写字母,例如customSelect将在html上写为custom-select

代码

var fancySelectDirective = pluginDirecitvesModule.directive("customSelect", function() {
    return {
        restrict: 'C',
        link: function(scope, element, attrs) {
            element.fancySelect();
        }
    }
});

希望这能帮到你,谢谢。