使用angular作一个项目,却又不是彻底使用的,不过也算用过了angular,里面一些常见问题,在此总结下html
var routeApp = angular.module('allApp',['ngAnimate']);
一、但愿$http是post传值,在头部添加web
routeApp.config(function($httpProvider){ $httpProvider.defaults.transformRequest = function(data) { //使用jQuery的param方法把JSON数据转换成字符串形式 return $.param(data); }; $httpProvider.defaults.headers.post = { 'Content-Type': 'application/x-www-form-urlencoded' } });
二、angular与fastclickjson
routeApp.run(function() { FastClick.attach(document.body); });
三、angular controller的做用域之间的通讯传值api
下面是子域之间的传值,不能直接传值,须要经过父域数组
//子域1 routeApp.controller('circleListCtl', function($scope, $http) { $scope.focusManager = function(id,$event){ $scope.$emit("focusmanager", id); }; }); //父域 routeApp.controller('parentCtl', function($scope) { $scope.$on("focusmanager",function (event, id) { $scope.$broadcast("focusmanagerFromParent", id); }); }); //子域2 routeApp.controller('focusManagerCtl', function($scope) { $scope.$on("focusmanagerFromParent",function (event, id) { //$scope.focus_show = true; //api.getuserid(id,$scope,"manager"); }); });
四、angular加载时会出现闪烁问题缓存
A将本来 <span>{{count}}</span> 这个写法改为 <span ng-bind="count"></span> 安全
我的以为但愿angular不闪烁只须要加一个 ng-cloak便可,ng-cloak须要添加样式session
[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak,.ng-hide{display:none !important;}ng\:form{display:block;}.ng-animate-start{clip:rect(0,auto,auto,0);-ms-zoom:1.0001;}.ng-animate-active{clip:rect(-1px,auto,auto,0);-ms-zoom:1;}
五、angular动画问题,如第一段代码,首先要加上angular-animate.js 头部引入ngAnimateapp
而后须要动画的地方加个类名,写上样式,这里以.repeated-item为例,是渐进渐出的样式,一般用在列表加载出来或者删除某一项时,就在要操做的元素上添加这个类名,样式以下: ide
.repeated-item{ &.ng-enter, &.ng-move { -webkit-transition:0.5s linear all; -moz-transition:0.5s linear all; -o-transition:0.5s linear all; transition:0.5s linear all; opacity:0; } &.ng-enter.ng-enter-active,&.ng-move.ng-move-active { opacity:1; } &.ng-leave { -webkit-animation:0.5s my_animation; -moz-animation:0.5s my_animation; -o-animation:0.5s my_animation; animation:0.5s my_animation; } } @keyframes my_animation { from { opacity:1; } to { opacity:0; } } @-webkit-keyframes my_animation { from { opacity:1; } to { opacity:0; } } @-moz-keyframes my_animation { from { opacity:1; } to { opacity:0; } } @-o-keyframes my_animation { from { opacity:1; } to { opacity:0; } }
我这个是经常使用,其余的没用到,有其余须要的能够在网上搜,用法差很少,就是ng-enter ng-move这几个,附上连接一个
http://www.tuicool.com/articles/jEvY3a
六、对于有些公用的地方,又不肯意写controller的,可使用$rootScope,做为全局的,在任何controller中均可以使用
routeApp.controller('parentCtl', function($scope,$rootScope) { 。。。。。。。。。。。。。 })
七、对于有些操做angular 的值不起做用的,能够添加 $apply
附上连接http://www.tuicool.com/articles/bAJVBvB
$scope.$apply(function(){ //添加列表 $scope.isowner = data.isowner; if(typeof($scope.subtopics) != 'undefined'){ $scope.subtopics = arr.concat($scope.subtopics, data.subject_list); }else{ $scope.subtopics = data.subject_list; } });
这个$apply里面的方法是把请求后的json结果放在$scope数据上,很明显$scope.subtopics这会是一个循环的列表,循环很简单
ng-repeat="topic in subtopics track by $index"
对于有些二维数组,须要嵌套使用的,有时会报错,由于$index 须要在后面加上 track by $index 就不会报错
八、最后加上一个删除某一项的代码
$scope.programs = $.grep($scope.programs, function(x) { return x.id != item.id; }); //删除 $scope.count -= 1; 等同 $scope.programs.forEach(function(v, i, _) { if (v.id == item.id) { $scope.programs.splice(i,1); $scope.count -= 1; } });
九、对于使用grunt压缩混淆angular的代码报错的问题
http://www.mincoder.com/article/1891.shtml
经实验,本来写的这种模式
routeApp.controller('subcommentlistCtl', function($scope,$rootScope) { })
改为
routeApp.controller('subcommentlistCtl', ['$scope', '$http', function ($scope, $http) {
}])
十、templateurl使用
http://www.cnblogs.com/haogj/p/3601528.html
restrict 的取值能够有三种:
routeApp.directive('allcomment', function() { return { restrict: 'E', templateUrl: topicapi.allcomment_url, replace:true, //替换掉本来的标签元素 } });
html里面写上<allcomment></allcomment>
这里使用有时会报错
Error: [$compile:tplrt] Template for directive 'allcomment' must have exactly one root element.
这种状况是templateurl里面内容不是一个总体,在外面套上<div></div>便可
十一、有时咱们须要在页面中输出含有 html 标签的字符串,但标签在页面上却被 angularJs 自动转义了,在页面上 html 标签不生效。(标签会转义成字符串在页面上输出)
好比咱们$scope.html = "<p>测试 </p>"; //页面输出 :“<p>测试 </p>”而不是“测试”
这里就须要使用 $sce 安全机制来输出 html
方法一,直接使用
routeApp.controller('subcommentlistCtl', ['$scope', '$http', '$sce',function ($scope, $http,$sce) { }])
不过方法一有时遇到link这类使用的时候很差用,最好使用方法二,把它封装成一个过滤器就能够在模板上随时调用了
方法二,把它封装成一个过滤器就能够在模板上随时调用了
routApp.filter('to_trusted', ['$sce', function ($sce) { return function (text) { return $sce.trustAsHtml(text); }; }]);
使用时html
<p ng-bind-html="html | to_trusted"></p>
13:angular的定时器 与缓存的简单使用
缓存须要引入js
头部须要
var myApp = angular.module('myApp',['ngAnimate','ngStorage','ui.sortable','ng.ueditor','ui.router']);
详情查看连接http://ngmodules.org/modules/ngStorage
myApp.controller('mainCtr', ['$scope', '$sessionStorage','$timeout',function ($scope, $sessionStorage,$timeout) { $timeout(function(){$scope.previewtip = false;},2000); //此时$timeout至关于setTimeout $scope.$storage = $sessionStorage.$default({ showmagazineset : true, form : {}, }); }])