Angular ng-animate和ng-cookies用法詳解
ng-animate
本文講一下Angular中動畫應(yīng)用的部分。
首先,Angular本生不提供動畫機制,需要在項目中加入Angular插件模塊ngAnimate才能完成Angular的動畫機制,Angular也不提供具體的動畫樣式,所以說,它的自由度和可定制性挺大的。
那么,剛開始需要在項目的入口html文件中引入Angular框架(angular.js),然后引入angular.animate.js。
在項目的js入口文件app.js中,新建項目模塊,并且添加所依賴的模塊ng-Animate(有其他需要的模塊的話也可以引入,順序沒關(guān)系)
var demoApp = angular.module('demoApp', ['ngAnimate','ui.router']);
這里中間插入一句,建議Angular中的依賴注入用如下模式,在ads、bds或者其他的前端自動化工具打包壓縮后不會產(chǎn)生問題,因為僅僅通過 給function傳參的形式注入依賴,Angular是會對注入的變量名有嚴(yán)格的要求(如$scope變量名在控制器中注入時變量名只能 寫$scope):
//控制器.js、指令.js、過濾器.js的依賴注入建議都用這種方式寫
//這是ui-route的配置,在app.js
demoApp.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider){
// your code.
}]);
好了,回到正題上。引入了ngAnimate之后,Angular的動畫機制就能生效了。
Angular文檔中寫到如下指令和支持的動畫

那么,怎么使用呢? 本文拿ng-repeat這個指令來做個介紹,其他的一些指令使用方式幾乎相同,可類推。
ng-repeat 主要是對一個list的展示,這些元素是是被創(chuàng)建出來加入到DOM結(jié)構(gòu)中去的,那么,它的動畫過程為:
創(chuàng)建元素 -> .ng-enter -> .ng-enter-active -> 完成,呈默認(rèn)狀態(tài)
默認(rèn)狀態(tài) -> .ng-leave -> .ng-leave-active -> 銷毀元素
所以可以通過設(shè)置.ng-enter(.ng-leave) 和 .ng-enter-active(.ng-leave-active) 的樣式,加上css3的動畫來顯示出動畫,如:
<!-- HTML片段 -->
<div ng-init="users = [1,2,3,4,5]"></div>
<input class="filter-btn" type="search" ng-model="u" placeholder="search item" aria-label="search item" />
<ul>
<li class="item" ng-repeat="user in users | filter: u as result">
{{user}}
</li>
</ul>
/* css片斷 */
/*ng-repeat的元素*/
.item{
-webkit-transition: all linear 1s;
-o-transition: all linear 1s;
transition: all linear 1s;
}
/*動畫開始前*/
.item.ng-enter{
opacity:0;
}
/*動畫過程*/
.item-ng-enter-active{
opacity:1;
}
這樣的效果是對所有元素同時應(yīng)用,可能實際運用中需要有一個先后的漸變出現(xiàn)的效果,這時候可以設(shè)置ng-enter-stagger來實現(xiàn).
/*不同時出現(xiàn)*/
.item.ng-enter-stagger {
transition-delay:0.5s;
transition-duration:0s;
}
同樣的,這些angular animate提供的動畫的class也可以應(yīng)用到頁面切換中去。自定義動畫(基于class)
在添加移除class時自定義動畫
.class-add -> .class-add-active -> .class
如果通過寫css的方式還無法滿足需求,當(dāng)然,還可以通過JS的方式來控制動畫,下面的代碼你可以理解為是一個模版
/* CLASS 是需要應(yīng)用的class名,handles是支持的操作,如下圖所示*/
/* 這里如果是應(yīng)用在ui-view 的class上,模版會疊加(坑)*/
demoApp.animation('.classname',function(){
return {
'handles':function(element,className,donw){
//... your code here
//回調(diào)
return function(cancelled){
// alert(1);
}
}
}
})
支持的操作:

ng-cookies
$cookies[name] = value;
這個是angular設(shè)置cookies方法
$cookieStore
提供一個被session cookies支持的鍵值對(字符串-對象)存儲。被存入和取出的對象將自動通過angular的toJson/fromJson進行序列化/反序列化。
$cookies
提供瀏覽器cookies的讀/寫訪問操作。
這兩個都要引入ngCookies模塊才能使用,這個模塊在1.4版本之后就有了
從源碼中得知$cookieStore返回了三個方法get put remove 他們分別用toJson/fromJson進行序列化/反序列化
簡單的寫了幾個例子來測試下
<!DOCTYPE html>
<html ng-app="AutumnsWindsApp" ng-controller="aswController">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<script src="http://code.angularjs.org/1.2.9/angular.min.js"></script>
<script src="http://code.angularjs.org/1.2.9/angular-cookies.min.js"></script>
<body>
{{title}}
</body>
<script>
var AutumnsWindsApp = angular.module('AutumnsWindsApp', ['ngCookies']);
AutumnsWindsApp.controller('aswController', function($cookies, $cookieStore, $scope) {
$cookies.name = 'autumnswind';
$scope.title = "Hello, i'm autumnswind :)";
$cookieStore.put("skill", "##");
//刪除cookies
$cookieStore.remove("name");
//設(shè)置過期日期
var time = new Date().getTime() + 5000;
$cookieStore.put("cookie", "Hello wsscat", {
expires: new Date(new Date().getTime() + 5000)
});
$cookieStore.put("objCookie", {
value: "wsscat cat cat",
age: "3",
}, {
expires: new Date(new Date().getTime() + 5000)
});
console.log($cookies);
console.log($cookies['objCookie']);
})
</script>
</html>
其實平時我們這樣就可以把自己需要的cookies設(shè)置進去
$cookies.name = 'autumnswind';
但是當(dāng)我們要設(shè)置一個有效時間的時候我們就用這樣的方法把它設(shè)置進去
var time = new Date().getTime() + 5000;
$cookieStore.put("cookie", "Hello wsscat", {
expires: new Date(new Date().getTime() + 5000)
});
我們還可以進行刪除等操作
$cookieStore.remove("name");
補充:
ng-repeat-track by用法:
<div ng-repeat="links in slides">
<div ng-repeat="link in links track by $index">{{link.name}}</div>
</div>
Error: [ngRepeat:dupes]這個出錯提示具體到題主的情況,意思是指數(shù)組中有2個以上的相同數(shù)字。ngRepeat不允許collection中存在兩個相同Id的對象
For example: item in items is equivalent to item in items track by $id(item). This implies that the DOM elements will be associated by item identity in the array.
對于數(shù)字對象來說,它的id就是它自身的值,因此,數(shù)組中是不允許存在兩個相同的數(shù)字的。為了規(guī)避這個錯誤,需要定義自己的track by表達(dá)式。例如:
item in items track by item.id或者item in items track by fnCustomId(item)。
嫌麻煩的話,直接拿循環(huán)的索引變量$index來用item in items track by $index
自定義服務(wù)的區(qū)別:
factory()----函數(shù)可以返回簡單類型、函數(shù)乃至對象等任意類型的數(shù)據(jù) 一般最為常用
service()-----函數(shù)數(shù)組、對象等數(shù)據(jù)
factory和service不同之處在于,service可以接收一個構(gòu)造函數(shù),當(dāng)注入該服務(wù)時通過該函數(shù)并使用new來實例化服務(wù)對象
constant()----value()方法和constant()方法之間最主要的區(qū)別是,常量可以注入到配置函數(shù)中,而值不行,value可與你修改,constant不能修改
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS基礎(chǔ) ng-paste 指令簡單示例
本文主要介紹AngularJS ng-paste 指令,這里對ng-paste 指令的基礎(chǔ)資料做了整理,并附有代碼示例,有需要的朋友可以參考下2016-08-08
angularJS+requireJS實現(xiàn)controller及directive的按需加載示例
本篇文章主要介紹了angularJS+requireJS實現(xiàn)controller及directive的按需加載示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
詳解AngularJS中$http緩存以及處理多個$http請求的方法
$http 是 AngularJS 中的一個核心服務(wù),用于讀取遠(yuǎn)程服務(wù)器的數(shù)據(jù),通過本文給大家介紹AngularJS中$http緩存以及處理多個$http請求的方法,希望的朋友一起學(xué)習(xí)吧2016-02-02
關(guān)于angular js_$watch監(jiān)控屬性和對象詳解
下面小編就為大家?guī)硪黄P(guān)于angular js_$watch監(jiān)控屬性和對象詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
探索angularjs+requirejs全面實現(xiàn)按需加載的套路
這篇文章主要探索了angularjs+requirejs全面實現(xiàn)按需加載的套路,圍繞angularjs提供的各種機制進行研究,感興趣的小伙伴們可以參考一下2016-02-02
Angular動畫實現(xiàn)的2種方式以及添加購物車動畫實例代碼
這篇文章主要給大家介紹了關(guān)于Angular動畫的2種方式以及添加購物車動畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

