指令名称和属性名称冲突 [英] Directive name and attribute name clashing

查看:21
本文介绍了指令名称和属性名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了指令和属性名称冲突的问题.这是我的问题的简化版本:有两个指令,其中第一个指令的名称是第二个指令的属性名称:

I encountered a problem with clashing directive and attribute names. This is a simplified version of my problem: there are two directives, where the name of the first directive is an attribute name of the second directive:

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 文件时:

When I try to add my second directive to an html file:

<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

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

Now, this wouldn't be a problem if both directives were in my modules, since renaming them would be easy. But I have clashing directive/attribute names from different external modules that I use in my application.

如何告诉 angular,属性 property 在这种特殊情况下不是指令?

How can I tell angular, that the attribute property is not meant to be a directive in this particular case?

推荐答案

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

Just extending my comment to answer, Instead of renaming the directive on way i could think of is to create a copy of the same directive and invalidate the existing one. That way you could have a proper naming conventions for poorly named directives that you are consuming from another module. Here you need

  • 注册一个新指令,覆盖angular指令构造函数app.directive.

$provide service

  • 要修饰名称不佳的指令,请获取其定义并返回一个空白的无操作工厂.
  • 您可以使用 Directive 关键字修饰指令 postFixing.他们也注册为工厂.
  • To decorate the directive with poor name, get its definition and return back just a blank no operation factory.
  • You can decorate a directive postFixing with Directive keyword. They are also registered as a factory.

你需要确定这个配置,特别是装饰部分出现在目标指令注册后.

You need to make sure this configuration, especially the decoration part appears after the targeted directives are registered.

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 };
    }]);
}]);

演示

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

You could automate this by placing the target directive names in a constant and running a loop of decorators to automatically prefix/rename(recreate with another name) it.

更新

在我的存储库中查看通用解决方案

这篇关于指令名称和属性名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆