第一次使用angularjs的directive指令蓖柔,遇到非常氣人的問題
app.directive('hello', function() {
return {
restrict: 'E',
// template: '<div>Hi there</div>',
templateUrl:'http://localhost:9090/keti/template/addKetiTemplate.html' ,
replace: true
};
});
/addKetiTemplate.html代碼如下:
<div>
some stuff
</div>
<div>
some other stuff
</div>
訪問報錯 Error: [$compile:tplrt]
解決方法:
在兩個div的外層添加一層div留荔,如下
<div>
<div>
some stuff
</div>
<div>
some other stuff
</div>
</div>
然后就可以了
templateUrl引用的模板文件,需要一個根節(jié)點或者說根元素劝堪。不能出現(xiàn)并列的html元素代碼贬墩,就比如兩行div呀闻,等等
并且注意
{{block.status === 1 ? "成功" : "失敗"}} 正確
{{block.status === 1 ? 成功 : 失敗}} 錯誤 會造成解析錯誤。
不過我們可以通過報錯得知具體的位置
angularjs [$parse:lexerr]
同時需要注意
指令名 <hello-world> 可以注冊為helloWorld
屬性名 <orderMethod> 在attrs中會被解析為ordermethod 全小寫秤涩。