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

<div  ng-controller="Aaa">
	<div>{{text}}</div>
    <div ng-bind="text"></div>
    <div ng-bind-template="{{text}},{{text}}"></div>
    <div ng-bind-html="html"></div>
    <div ng-bind-html="currentWork.description"></div>
    <div ng-bind-html="content|to_trusted"></div>
</div>
<script>
var m1=angular.module('bind',[]);

m1.controller('Aaa',['$scope','$sce',function($scope,$sce){
	$scope.text="123"
	$scope.html=$sce.trustAsHtml("<h1>123</h1>");
	$scope.currentWork=[];
	$scope.currentWork.description = $sce.trustAsHtml("hello,<br><b>今天我們去哪裏?</b>");
	
	$scope.content="<p>信任內容!</p>";
}]);

m1.filter('to_trusted',["$sce",function($sce){
	return function(text){
		return $sce.trustAsHtml(text);	
	}
}]);
</script>


發佈了7 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章