1

I have a simple string as follows:

var templateString = "<span>This is a test</span>" 

This string is defined inside a directive's link function.

Now, inside the link function, I carry out the following code:

scope.$eval(templateString); 

My next step is to $compile the data and associate it with the scope.

However, I get the error when I do the $eval:

Uncaught Error: Syntax Error: Token 'span' is an unexpected token at column 2 of the expression [<span>This is a test</span>] starting at [span>This is a Test</span>]. 

But if I look at the documentation located here, I seem to have carried out the steps correctly yet the string does not evaluate.

EDIT: I am using the following example from the documentation:

 angular.module('compile', [], function($compileProvider) { // configure new 'compile' directive by passing a directive // factory function. The factory function injects the '$compile' $compileProvider.directive('compile', function($compile) { // directive factory creates a link function return function(scope, element, attrs) { scope.$watch( function(scope) { // watch the 'compile' expression for changes return scope.$eval(attrs.compile); }, function(value) { // when the 'compile' expression changes // assign it into the current DOM element.html(value); // compile the new DOM and link it to the current // scope. // NOTE: we only compile .childNodes so that // we don't get into infinite loop compiling ourselves $compile(element.contents())(scope); } ); }; }) }); 

I am not using the $watch however since I don't need to watch any expression and have the template with me already (templateString).

2
  • The documentation does not seem to mention $eval anywhere. You should directly $compile the template and provide it the scope to create an element, you should not need any $eval unless you are obtaining that string through the elements attrs. Commented Mar 13, 2014 at 17:15
  • @musically_ut I updated my question to indicate the example that I am making use of. If I don't evaluate, then any angular expression within span tag is shown as it is without evaluating it first... I want the angular expressions (if any) to be evaluated too before I compile it. Commented Mar 13, 2014 at 17:24

1 Answer 1

4

$eval is to evaluate an expression, your templateString isn't a valid expression, that's why the error occurs.

You should use just $compile(templateString)(scope), it will compile your template, and link with scope, means that all expression will be evaluated with the supplied scope.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I add the compiled template to an element? I mean, after execution of $compile, I wish to add the compiled template to a specific location in the DOM - how do I do that?
Nevermind, I got my answer about adding the compiled template here. Thanks for your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.