[英]Best way to extend third party libraries in AngularJs
I am using the angular bootstrap ui third party library as a dependency inside my angular app.我在我的 angular 应用程序中使用 angular bootstrap ui 第三方库作为依赖项。 I was just wondering what is the best way to add functionality to directives and controllers inside this library?我只是想知道向这个库中的指令和控制器添加功能的最佳方法是什么?
I understand that I can just edit the directives/controllers inside ui-bootstrap-tpls-0.11.0.js, but if I were to re pull the dependencies on a build server, it would wipe away my changes.我知道我只能编辑 ui-bootstrap-tpls-0.11.0.js 中的指令/控制器,但是如果我要重新拉取构建服务器上的依赖项,它会清除我的更改。 If I were to update the library version it would also wipe away my changes.如果我要更新库版本,它也会抹去我的更改。 I'm looking for a clean way to extend functionality.我正在寻找一种干净的方法来扩展功能。
For example, if I want to do something like extend the datepicker directive to accept a customMethod or customData then use these within the linking function.例如,如果我想做一些类似扩展 datepicker 指令以接受 customMethod 或 customData 的事情,然后在链接函数中使用它们。 What is the best way to do this?做这个的最好方式是什么?
<datepicker ng-model="dt" custom-method="myCustomMethod()"
custom-attribute="myCustomAttribute" min-date="minDate"
show-weeks="true" class="well well-sm"></datepicker>
Thanks in advance.提前致谢。
One option is to decorate the directive.一种选择是装饰指令。 Decoration looks something like:装饰看起来像:
angular.module('app', ['ui.bootstrap']).
config(function($provide){
// Inject provide into the config of your app
$provide.decorator('datepickerDirective', function($delegate){
// the directive is the first element of $delegate
var datepicker = $delegate[0];
// Add whatever you want to the scope:
angular.extend(datepicker.scope, {
customAttribute: '@',
customMethod: '@'
});
// Might want to grab a reference to the old link incase
// you want to use the default behavior.
var oldLink = datepicker.link;
datepicker.link = function(scope, element, attrs){
// here you can write your new link function
oldLink(scope, element, attrs);
};
return $delegate;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.