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).
$evalanywhere. You should directly$compilethe template and provide it the scope to create anelement, you should not need any$evalunless you are obtaining that string through the elementsattrs.spantag is shown as it is without evaluating it first... I want the angular expressions (if any) to be evaluated too before I compile it.