AngularJs遇到的小坑与技巧

1. templateURL和路由之类的要在web server下运行。html

2. 使用模板replace设为true,模板里也要有相应的标签,不然不出现任何数据。jquery

3. 1.2版本以后,ngRoute模块独立。linux

4.空的controller不定义会出错。git

5.Directive的link参数是有顺序的:scope,element,attrs,ctrlgithub

6.ng-repeat不能循环重复的对象。hack: ng-repeat="thing in things track by $id($index)"web

7.尽可能更新的是变量的属性而不是单个变量自己。app

8.注意ng-repeat,ng-controller等会产生独立做用域。this

9.当jquery载入,则使用jquery,不然使用内置jqlite。all element references in Angular are always wrapped with jQuery or jqLite; they are never raw DOM references.code

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

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.)