加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 指令名称和属性名称冲突

发布时间:2020-12-17 07:49:17 所属栏目:安全 来源:网络整理
导读:我遇到了冲突指令和属性名称的问题.这是我的问题的简化版本:有两个指令,其中第一个指令的名称是第二个指令的属性名称: angular.module('mymodule').directive('property',function() { return { template: 'divProperty Directive/div' };});angular.modul
我遇到了冲突指令和属性名称的问题.这是我的问题的简化版本:有两个指令,其中第一个指令的名称是第二个指令的属性名称:
angular.module('mymodule').directive('property',function() {
    return {
        template: '<div>Property Directive</div>'
    };
});

angular.module('mymodule').directive('fail',function() {
    return {
        scope: {
            property: '='
        },template: '<div>{{property}}</div>'
    }
});

当我尝试将第二个指令添加到html文件时:

<fail property="'test'"></fail>

我收到以下错误:

Error: [$compile:multidir] Multiple directives [fail,property] asking for template on: <fail property="'test'">http://errors.angularjs.org/1.3.0-rc.4/$compile/multidir?p0=fail&p1=property&p2=template&p3=%3Cfail%20property%3D%22'test'%22%3E

现在,如果两个指令都在我的模块中,这不会成为问题,因为重命名它们很容易.但是我在我的应用程序中使用了来自不同外部模块的指令/属性名称.

如何判断angular,在这种特殊情况下属性属性不是指令?

只是扩展我的评论来回答,而不是在我想到的方式上重命名指令是创建同一指令的副本并使现有指令无效.这样,您可以为从另一个模块使用的命名不佳的指令具有适当的命名约定.在这里你需要

> $compileProvider

>要注册新指令,请覆盖angular指令构造函数app.directive.

> $provide service

>要装饰名称不佳的指令,获取其定义并返回一个空白的无操作工厂.
>您可以使用Directive关键字修饰指令postFixing.他们也注册为工厂.

您需要确保此配置,尤其是在注册目标指令后出现装饰部分.

app.config(['$compileProvider',function ($compileProvider) {
    //Override directive constructor
    app.directive = function (name,dirObject) {
        //Register a directive
        $compileProvider.directive(name,function() {
           return dirObject[0];
        });
    };
}]).config(['$provide',function($provide){
    //Decorate target directive
    $provide.decorator('propertyDirective',['$delegate',function($delegate){
        //Just register a new directive with source's definition
        app.directive('cmProperty',$delegate);
        //return a no operation factory as directive constructor,to make it inactive
        return function() { return angular.noop };
    }]);
}]);

Demo

您可以通过将目标指令名称放在常量中并运行装饰器循环来自动添加/重命名(使用其他名称重新创建)来自动执行此操作.

更新

见generic solution in my repository

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读