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
}
}
})
But this spits out the error
Error: Unknown provider: $interpolateProviderProvider <- $interpolateProvider <- underscoreTemplateDirective
Is $interpolateProvider
only available for module configuration? Would a better solution be to simply using string replace to change <%=
to {{
and %>
to }}
?
Also, I noticed that element.html()
escapes the <
in <%=
and >
in %>
. Is there a way to prevent this automatic escaping?
AngularJS Interpolation 1 Interpolation markup with embedded expressions will provide data-binding to text nodes and attribute values. 2 The AngularJS allows the user to manually run the template compilation. 3 Interpolation allows us to live update a string of text based upon conditions of a scope, for instance. More items...
& binding is for passing a method into your directive's scope so that it can be called within your directive. When we are setting scope: true in directive, Angular js will create a new scope for that directive. That means any changes made to the directive scope will not reflect back in parent controller. Show activity on this post.
In an AngularJS directive the scope allows you to access the data in the attributes of the element to which the directive is applied. This is illustrated best with an example:
This information is present in the AngularJS directive documentation page, although somewhat spread throughout the page. The symbol > is not part of the syntax. However, < does exist as part of the AngularJS component bindings and means one way binding.
Ok, you have a couple issues here, but I found a solution for you.
http://jsfiddle.net/colllin/zxwf2/
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.
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);
});
}
}
});
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With