AngularJs遇到的小坑与技巧
2016-07-06 10:03
471 查看
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>
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>
相关文章推荐
- angularJS
- Angularjs理解三-$location
- Angularjs总结(八)$ cookie和$rootscope
- Angularjs总结(七) 路由及请求服务等
- angular input标签只能单向传递数据的问题
- 使用Angular Resources 封装Rest API
- angularjs + jqgrid 实现表格
- AngularJS ui-router (嵌套路由)
- Angularjs-2 (basic)
- angularjs - 415 (Unsupported Media Type)
- angular学习(四)—— Services
- angularJs表单校验(超级详细!!!)
- 转:AngularJS跨页面传值
- angularJS的controller之间如何正确的通信
- 优化Angular应用的性能
- AngularJs的自定义的正则校验指令--代码共享
- AngularJS实例教程(二)
- AngularJS实例教程(一)
- 基于 angularjs+jfinal 的 excel文件导出
- UVA 10825 Yummy Triangular Pizza (暴力)