Javascript Angular 1.3.15在第三方指令中覆盖变量
我已经决定在我当前的项目中使用。在它的指令中有一个Javascript Angular 1.3.15在第三方指令中覆盖变量,javascript,angularjs,Javascript,Angularjs,我已经决定在我当前的项目中使用。在它的指令中有一个var=templateString,我想将其编辑为我选择的模板 我想知道如何编辑这个字符串而不干扰原始代码。我读了一系列类似的答案,但最后我发现是创建一个完全覆盖它的指令。我只想编辑模板字符串并保留现有代码 我用的是角度1.3.15 指令 MessageCenterModule。 指令('mcMessages',['$rootScope','messageCenterService',函数($rootScope,messageCenterSe
var=templateString
,我想将其编辑为我选择的模板
我想知道如何编辑这个字符串而不干扰原始代码。我读了一系列类似的答案,但最后我发现是创建一个完全覆盖它的指令。我只想编辑模板字符串并保留现有代码
我用的是角度1.3.15
指令
MessageCenterModule。
指令('mcMessages',['$rootScope','messageCenterService',函数($rootScope,messageCenterService){
/*jshint multistr:true*/
var templateString='1〕\
\
\
&时代\
\
\
\
\
\
{{message.message}}\
\
\
\
';
返回{
限制:“EA”,
模板:templateString,
链接:函数(范围、元素、属性){
//将服务中的消息绑定到根作用域。
messageCenterService.flush();
var CHANGERACTION=函数(事件、到、从){
//更新标记为“已显示”的“未看到”消息。
messageCenterService.markShowed();
//删除已显示的消息。
messageCenterService.removeShown();
$rootScope.mcMessages=messageCenterService.mcMessages;
messageCenterService.flush();
};
if(messageCenterService.offlistener==未定义){
messageCenterService.offlistener=$rootScope.$on(“$locationChangeSuccess”,changeReaction);
}
scope.animation=attrs.animation | |“淡入”;
}
};
}]);代码>您可以使用AngularJS装饰器修改指令,如下所示:
MessageCenterModule
.config(function ($provide) {
$provide.decorator('mcMessagesDirective', function ($delegate) {
var directive = $delegate[0];
//assign a new value to the template
directive.template = 'My template';
return $delegate;
});
});
我很抱歉,朋友,你必须重写它
哟你要装饰它。听起来有点时髦,但是,嘿。它起作用了
无论何时使用,都会通过名称$delegate
将原始实例作为本地可注入实例
因此,你可以保留你想要的,扔掉你不想要的
您首先要做的是弄清楚原始实现如何利用您想要修改的内容,从而不破坏整个过程
幸运的是,您要修改的仅用作指令.template
,因此它应该是一个相当简单的修饰
事情会是这样的:
app.config(function ($provide) {
/**
* note the use of 'directivename' + 'Directive'.
* Whenever you decorate a directive, you have to apply the 'Directive' suffix due to this:
* https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L727
*/
$provide.decorator('mcMessagesDirective', function ($delegate) {
/**
* We're getting the item at the first index here,
* because the $delegate of a directive isn't quite an 'instance' -
* it's a collection of DDO's (directive definition objects) that
* go by the same name.
*
* Yes, the $compileProvider allows multiple directives with the same name.
* We're interested in the first one in this case.
*/
var dir = $delegate[0];
/**
* Modify the template of the directive.
* You can either hardcode this, or:
* - Decorate the directive so as to pass the template in.
* - Fetch a template with $http.
* - Inject a string from a service, constant, provider, you name it.
*/
dir.template = 'your_own_custom_templateString';
/**
* Return the full collection of directives (or rather, 'the $delegate').
*/
return $delegate;
});
});
现在,每当您再次使用mcMessages
时,它都会使用您刚才提供的硬编码模板
摩尔链接
- (有正确的方法,也有不太正确的方法)
嘘,看起来好像有人打败了我!:+1:给你,先生。只需几秒钟,我就可以+1d你的,因为你提供了链接。抛出一个错误<代码>错误:[$injector:modulerr]未能实例化模块收藏夹,原因:[$injector:modulerr]未能实例化模块MessageCenterModule,原因:[$injector:unpr]未知提供程序:mcMessageProvider
仅当包含上述代码时。据我所知,这种方法不再适用于角度1.3+Oops,犯了一个错误:它是$provide.decorator('mcMessagesDirective',它需要一个后缀per。我编辑了我的答案。