AngularJs遇到的小坑與技巧


1. templateURL和路由之類的要在web server下運行。

2. 使用模板replace設為true,模板里也要有相應的標簽,否則不出現任何數據。

3. 1.2版本之后,ngRoute模塊獨立。

4.空的controller不定義會出錯。

5.Directive的link參數是有順序的:scope,element,attrs,ctrl

6.ng-repeat不能循環重復的對象。hack: ng-repeat="thing in things track by $id($index)"

7.盡量更新的是變量的屬性而不是單個變量本身。

8.注意ng-repeat,ng-controller等會產生獨立作用域。

9.當jquery載入,則使用jquery,否則使用內置jqlite。all element references in Angular are always wrapped with jQuery or jqLite; they are never raw DOM references.

10.Uncaught Error: [$location:ihshprfx]  A標簽沒有去掉 <a href="#" ng-click="someMethod();"></a>

11.Error: listen EACCES 當在linux下,會出現這個錯誤,因為你監聽的端口的原因,這里我的是33。把它改成8080或3030之類大的端口數就可以了。有一個規定,這些端口最好是大於1024。

12. select在沒有ng-model的時候,無法顯示。同理,當遇到無法顯示最好看文檔少了什么。

補:當ng-options的源,跟書寫不相配時會出現全部選擇的情況,如下:

var a = [{"id":1,"name":"Ryan"}....] ,ng-options="item.i as item.name for item in a"  // i與id不同

----------------------------------------------------------------------------------------

13.ng-bind-html-unsafe已去除,可以用['ngSanitize'] 模塊或使用$sce服務

From stackoverflow

You indicated that you're using Angular 1.2.0... as one of the other comments indicated, ng-bind-html-unsafe has been deprecated.

Instead, you'll want to do something like this:

<div ng-bind-html="preview_data.preview.embed.htmlSafe"></div>

In your controller, inject the $sce service, and mark the HTML as "trusted":

myApp.controller('myCtrl', ['$scope', '$sce', function($scope, $sce) { // ... $scope.preview_data.preview.embed.htmlSafe = $sce.trustAsHtml(preview_data.preview.embed.html); }

Note that you'll want to be using 1.2.0-rc3 or newer. (They fixed a bug in rc3 that prevented "watchers" from working properly on trusted HTML.)

 

 

持續更新中...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM