Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs use custom interpolation symbols for scope

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?

like image 950
JD White Avatar asked Aug 23 '13 19:08

JD White


People also ask

What is the use of interpolation in angular?

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...

What is the use of @&binding in AngularJS?

& 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.

What is the scope of an AngularJS directive?

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:

What is the syntax for < symbol in AngularJS directives?

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.


1 Answers

Ok, you have a couple issues here, but I found a solution for you.

Demo

http://jsfiddle.net/colllin/zxwf2/

Issue 1

Your < and > characters are being converted to &lt; and &gt;, so when you call element.html(), you won't even find an instance of < or > in that string.

Issue 2

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.

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(/&lt;%=/g, startSym).replace(/&lt;%-/g, startSym).replace(/%&gt;/g, endSym);
            var parsedExp = $interpolate(transformedExp);

            scope.$watch(parsedExp, function(newValue) {
                element.html(newValue);
            });
        }
    }
});

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).

like image 89
colllin Avatar answered Oct 22 '22 15:10

colllin