2014-03-13 1 views
1

나는 간단한 문자열이있다.

scope.$eval(templateString); 

내 다음 단계는 $compile 데이터에이고 범위와 연결 :

는 이제 link 함수 내에서, 나는 다음과 같은 코드를 실시하고 있습니다. 나는 $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>]. 

을하지만 here있는 문서를 보면, 나는 제대로 아직 문자열이 평가하지 않는 단계를 수행 한 것으로 보인다.

편집 : 나는 문서에서 다음 예제를 사용하고 있습니다 : 나는 어떤 표정을보고 이미 저와 템플릿이 필요하지 않기 때문에 그러나 $watch를 사용하고 있지 않다

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); 
      } 
     ); 
     }; 
    }) 
    }); 

(templateString) .

+0

설명서에 '$ eval'은 어디에도 언급되어 있지 않습니다. 템플리트를 직접'$ compile '하고'element'를 생성하는 범위를 제공해야합니다.'attrs' 요소를 통해 문자열을 얻지 않는 한'$ eval'은 필요 없습니다. –

+0

@musically_ut 내가 사용하고있는 예제를 나타 내기 위해 내 질문을 업데이트했습니다. 평가하지 않으면'span' 태그 안에있는 각 표현식이 먼저 평가되지 않고 그대로 표시됩니다. 컴파일하기 전에 각도 표현식 (있는 경우)을 평가하기를 원합니다. – callmekatootie

답변

4

$eval은 expression을 평가하는 것이므로 templateString이 유효한 표현식이 아니므로 오류가 발생합니다.

$compile(templateString)(scope)을 사용하고 템플릿을 컴파일하면 범위가 이고 모든 표현식이 제공된 범위로 평가됩니다.

+0

컴파일 된 템플릿을 요소에 어떻게 추가합니까? 나는'$ compile '을 실행 한 후 컴파일 된 템플릿을 DOM의 특정 위치에 추가하고자합니다. 어떻게해야합니까? – callmekatootie

+0

신경 쓰지 마, 나는 컴파일 된 템플릿 [여기] (http://stackoverflow.com/questions/14846836/insert-an-angular-js-template-string-inside-an-element) 추가에 대한 내 대답을 얻었다. 귀하의 답변에 감사드립니다. – callmekatootie