1 <ul class="phones"> 2 <li ng-repeat="phone in phones | filter:query | orderBy:orderProp" class="thumbnail"> 3 <a href="#/phones/{{phone.id}}" class="thumb"><img src="{{phone.imageUrl}}"></a> 4 <a href="#/phones/{{phone.id}}">{{phone.name}}</a> 5 <p>{{phone.snippet}}</p> 6 </li>
7 </ul>
如果我們僅僅用一個正常src
屬性來進行綁定(<img class="diagram" src="{{phone.imageUrl}}">
),這個表達式被放入src的話,最終得到的圖片URL為:http://localhost:8000/app/{{phone.imageUrl}}
。這是因為瀏覽器會把AngularJS的{{ 表達式 }}
標記直接進行字面解釋,並且發起一個向非法的請求。
棒棒的AngularJS為我們提供了ngSrc
命令(對應ng-src
屬性),有了這個ngSrc
指令會避免產生這種情況,使用ngSrc
指令防止瀏覽器產生一個指向非法地址的請求。
1 <ul class="phones"> 2 <li ng-repeat="phone in phones | filter:query | orderBy:orderProp" class="thumbnail"> 3 <a href="#/phones/{{phone.id}}" class="thumb"><img ng-src="{{phone.imageUrl}}"></a> 4 <a href="#/phones/{{phone.id}}">{{phone.name}}</a> 5 <p>{{phone.snippet}}</p> 6 </li> 7 </ul>
趕快試試吧