2015-04-03 99 views
0

似乎是AngularJS在HTML模板的第一行處理註釋時出現問題。AngularJS指令模板開頭的註釋

控制檯使我有以下錯誤:

Error: [$compile:tplrt] http://errors.angularjs.org/1.3.15/$compile/tplrt?p0=deviceItem&p1=deviceItem.html 
    at Error (native) 
    at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js:6:417 
    at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js:65:275 
    at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js:112:113 

所以不要在AngularJS指令的第一行中使用的註釋,來避免這個問題。

有人知道爲什麼或者它只是一個錯誤嗎?

回答

2

的鏈接解釋了爲什麼這是一個錯誤:

When a directive is declared with template (or templateUrl) and replace mode on, the template must have exactly one root element.

評論和文本節點仍然即使他們不是標籤元素。因此,你不能有:

<!-- comment node --> 
non-comment node 

Angular仍然可以使用註釋以及指令。

您不能使用replace作爲指令定義的一部分,或者只是將註釋移動到根標記中。我不認爲這是如此糟糕:

<div> 
    <!-- directive comments --> 
    other things 
</div> 
+0

使用替換指令不幫的定義,但移動幫助。感謝您使用錯誤消息中的鏈接進行啓發。我沒有意識到這解釋了這個問題。 – dec 2015-04-03 20:54:45