javascript - Angularjs use custom interpolation symbols for scope -
i have underscore.js template use angular , still able use underscore. wondering if it's possible change interpolation start , end symbols particular scope using directive, 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()); // replace element contents interpolated contents } } })
but spits out error
error: unknown provider: $interpolateproviderprovider <- $interpolateprovider <- underscoretemplatedirective
is $interpolateprovider
available module configuration? better solution using string replace change <%=
{{
, %>
}}
?
also, noticed element.html()
escapes <
in <%=
, >
in %>
. there way prevent automatic escaping?
ok, have couple issues here, i found solution you.
demo
http://jsfiddle.net/colllin/zxwf2/
issue 1
your <
, >
characters being converted <
, >
, when call element.html()
, won't find instance of <
or >
in string.
issue 2
since $interpolate
service has been "provided" $interpolateprovider
, doesn't can edit startsymbol , endsymbol. however, can convert custom startsymbol , endsymbol angular start/end symbols dynamically in linking function.
solution
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); }); } } });
alternatives
i'm not sure how, i'm sure there's way instantiate own custom $interpolate
service using $interpolateprovider
(after configuring underscore tags).
Comments
Post a Comment