2017-04-08 52 views
0
Syntax Error: Token '<' not a primary expression at column 1 of the expression [<div>I think happiness is key to everything</div>] starting at [<div>I think happiness is key to everything</div>]. 

html 
-------------------- 
<p dynamic="<div>I think happiness is key to everything</div>"></p> 

directive 
--------------------- 
app.directive('dynamic', function($compile){ 
    return function(scope, element, attrs) { 
    scope.$watch(
     function(scope) { 
      return scope.$eval(attrs.dynamic); 
     }, 
     function(value) { 
      element.html(value); 
      $compile(element.contents())(scope); 
     } 
    ); 
    }; 
}); 

不知道爲什麼會產生錯誤。我嘗試了角度錯誤頁面,但找不到合適的答案。我用這個angular ng-bind-html and directive within it來解決我的問題,但它會引發錯誤。任何幫助是極大的讚賞。謝謝

回答

1

http://jsfiddle.net/4crh12zm/

<p dynamic="'<div>I think happiness is key to everything</div>'"></p> 

您嘗試評估定表達式,但你要表達的其實是一個字符串,所以你必須把它作爲一個字符串。

因此,爲您傳遞給「動態」的值添加引號