Angularjs使用自定义插值符号作为范围 [英] Angularjs use custom interpolation symbols for scope
问题描述
我目前有一个underscore.js模板,我也想使用角度,仍然可以使用下划线。我想知道是否可以使用指令更改特定范围的插值开始和结束符号,如下所示:
I currently have an underscore.js template that I would also like to use with angular and still be able to use with underscore. I was wondering if it's possible to change the interpolation start and end symbols for a particular scope using a directive, like this:
angular.directive('underscoreTemplate', function ($parse, $compile, $interpolateProvider, $interpolate) {
return {
restrict: "E",
replace: false,
link: function (scope, element, attrs) {
$interpolateProvider.startSymbol("<%=").endSymbol("%>");
var parsedExp = $interpolate(element.html());
// Then replace element contents with interpolated contents
}
}
})
但这会吐出错误
错误:未知提供者:$ interpolateProviderProvider< - $ interpolateProvider< - underscoreTemplateDirective
Error: Unknown provider: $interpolateProviderProvider <- $interpolateProvider <- underscoreTemplateDirective
$ interpolateProvider
仅适用于模块配置?更好的解决方案是使用字符串替换来将<%=
更改为 {{
和%>
到}}
?
Is $interpolateProvider
only available for module configuration? Would a better solution be to simply using string replace to change <%=
to {{
and %>
to }}
?
另外,我注意到了 element.html()
在<%= $ c $中转义
<
c>和>
在%>
中。有没有办法防止这种自动转义?
Also, I noticed that element.html()
escapes the <
in <%=
and >
in %>
. Is there a way to prevent this automatic escaping?
推荐答案
好的,你有几个问题,但是我找到了解决方案。
Ok, you have a couple issues here, but I found a solution for you.
http://jsfiddle.net/colllin/zxwf2/
http://jsfiddle.net/colllin/zxwf2/
您的<
和>
字符正在转换为& lt;
和& gt;
,因此当您调用<$ c $时c> element.html(),你甚至找不到<
或> 的实例该字符串中的code>。
Your <
and >
characters are being converted to <
and >
, so when you call element.html()
, you won't even find an instance of <
or >
in that string.
由于 $ interpolate
服务已经由 $ interpolateProvider
提供,看起来你可以编辑startSymbol和endSymbol。 但是,您可以在链接功能中动态地将自定义startSymbol和endSymbol转换为角度开始/结束符号。
Since the $interpolate
service has already been "provided" by the $interpolateProvider
, it doesn't look like you can edit the startSymbol and endSymbol. However, you can convert your custom startSymbol and endSymbol to the angular start/end symbols dynamically in your linking function.
myApp.directive('underscoreTemplate', function ($parse, $compile, $interpolate) {
return {
restrict: "A",
link: function(scope, element, attrs) {
var startSym = $interpolate.startSymbol();
var endSym = $interpolate.endSymbol();
var rawExp = element.html();
var transformedExp = rawExp.replace(/<%=/g, startSym).replace(/<%-/g, startSym).replace(/%>/g, endSym);
var parsedExp = $interpolate(transformedExp);
scope.$watch(parsedExp, function(newValue) {
element.html(newValue);
});
}
}
});
替代品
我不确定如何,但我确信有一种方法可以使用 $ interpolateProvider
来实例化你自己的自定义 $ interpolate
服务(之后)为下划线标签配置它。)
Alternatives
I'm not sure how, but I'm sure there's a way to instantiate your own custom $interpolate
service using the $interpolateProvider
(after configuring it for underscore tags).
这篇关于Angularjs使用自定义插值符号作为范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!