![](/img/trans.png)
[英]What is the best practice for angularjs and directives in the given workflow?
[英]What's the best way to fix two AngularJS directives with the same name?
我有一个项目变得非常庞大。 我的问题是因为......不明智的......选择的指令名称,即“datepicker”,因此会发生冲突的第三方指令。
由于我使用bower进行依赖关系管理,我不想编辑任何库,因为这会破坏可移植性。
怎么有人解决了这个问题?
在我的评论之后,我创建了一个plunker,看看这个解决方法是否可行,并看看Angular在名称冲突但在不同模块下的行为:
http://plnkr.co/edit/9JKTEfGG4bu47QQIEhBh?p=preview
似乎如果对不同的指令使用不同的限制,那么它确实有效(你需要使用不同指令上不相互的指令)。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl as vm">
<div some-directive></div>
<some-directive></some-directive>
<script>
var myApp2 = angular.module('myApp2',[]);
myApp2.directive("someDirective", function() {
return {
restrict: 'E',
template: 'inside myApp2'
};
});
var myApp = angular.module('myApp', ['myApp2']);
myApp.directive("someDirective", function() {
return {
restrict: 'A',
template: 'inside myApp'
};
});
</script>
</body>
</html>
OUTPUT
inside myApp
inside myApp2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.