AngularJS Phonecat實(shí)例講解
前言
最近關(guān)于A(yíng)ngularJS的資料也看了一些,其官網(wǎng)上有個(gè)實(shí)例Phonecat很不錯(cuò),硬著頭皮看了一會(huì)實(shí)在是看不下去,索性自己動(dòng)手實(shí)現(xiàn)下,遇到問(wèn)題時(shí)再?gòu)睦锩鎸ふ掖鸢敢膊皇橐环N好方法。說(shuō)的再多、看的再多不如自己動(dòng)手去做一遍,那就開(kāi)始吧。
正文
1、布局
布局很簡(jiǎn)單,首頁(yè)側(cè)邊欄是一個(gè)輸入框和下拉框,右邊是一個(gè)列表,實(shí)現(xiàn)時(shí)對(duì)首頁(yè)不做大的修改。詳情頁(yè)做一些改變,盡量做一些簡(jiǎn)化,考慮加一個(gè)自定義指令(輪播圖)。
2、架構(gòu)分析
首先思考一下需要用到的服務(wù)。
由于我們要做的是一個(gè)單頁(yè)應(yīng)用,所以需要服務(wù)$route、$location。利用服務(wù)$resource獲取資源。利用服務(wù)$filter對(duì)首頁(yè)數(shù)據(jù)過(guò)濾以及排序??偨Y(jié)一下:
1).服務(wù)$route、$location負(fù)責(zé)路由管理及跳轉(zhuǎn)。
2).服務(wù)$resource負(fù)責(zé)資源的獲取。
3).服務(wù)$filter負(fù)責(zé)數(shù)據(jù)的過(guò)濾及排序。
3、首頁(yè)及詳情頁(yè)view視圖
1、首頁(yè)視圖
<div class="main">
<div class="side">
<p>
<label>Search:</label>
<input ng-model="filterKey" type="text" style="width:150px; ">
</p>
<p>
<label>Sort by:</label>
<select ng-model="sortKey">
<option value="price">price</option>
<option value="name" ng-selected="true">name</option>
</select>
</p>
</div>
<div class="content">
<ul>
<li ng-repeat="item in data" ng-click="$location.path('detail/'+item.id)">
<img ng-src={{item.img}}>
<div>
<h2>名字:{{item.name}}</h2>
<p>性能:{{item.title}}</p>
<p>價(jià)格:{{item.price | currency}}</p>
</div>
</li>
</ul>
</div>
</div>
2、詳情頁(yè)視圖
<slide></slide>是一個(gè)自定義指令,實(shí)現(xiàn)輪播圖的功能
<div class="detail">
<slide links='links' w='200' h='200'></slide>
<div class="text">
<h2>設(shè)備:{{data.name}}</h2>
<p>性能:{{data.desc}}</p>
</div>
</div>
4、邏輯分析
1、首先說(shuō)明下外部資源infor.json的信息。是一個(gè)數(shù)組,數(shù)組每一項(xiàng)為一個(gè)json對(duì)象,含有以下字段:
{
"id" : 1,
"name" : "aaa",
"title" : "bbb",
"desc" : "ccc",
"img" : "img/a.jpg",
"price" : 100,
"showList" : "[{"url":"img/b.jpg"},{"url":"img/c.jpg"}]"
}
2、路由管理($route)
m1.config(['$routeProvider',function($routeProvider){
$routeProvider
.when('/index',{
templateUrl : 'template/index.html',
controller : 'index'
})
.when('/detail/:str',{
templateUrl : 'template/detail.html',
controller : 'detail'
})
.otherwise({
redirectTo : '/index'
});
}]);
當(dāng)形如http://localhost/phonecat/phone.html#/index加載index模板
當(dāng)形如http://localhost/phonecat/phone.html#/detail/0加載detail模板
默認(rèn)為http://localhost/phonecat/phone.html#/index
3、首頁(yè)(index)邏輯分析
首頁(yè)資源加載:
var arr = [];
var objRe = $resource('infor.json');
$scope.data = arr = objRe.query(); //獲得data數(shù)據(jù)后首頁(yè)即可進(jìn)行渲染
首頁(yè)數(shù)據(jù)的過(guò)濾及排序:
$scope.$watch('filterKey',function(){ //監(jiān)聽(tīng)輸入框的數(shù)據(jù)變化,完成數(shù)據(jù)的篩選
if($scope.filterKey){
$scope.data = $filter('filter')(arr,$scope.filterKey);
}else{
$scope.data = arr;
}
})
$scope.$watch('sortKey',function(){ //監(jiān)聽(tīng)select下拉框的數(shù)據(jù)變化,完成數(shù)據(jù)的排序
if($scope.sortKey){
$scope.data = $filter('orderBy')($scope.data,$scope.sortKey);
}else{
$scope.data = arr;
}
})
//這里有個(gè)需要注意的地方,我們用一個(gè)數(shù)組arr作為媒介,避免bug
點(diǎn)擊列表進(jìn)行詳情頁(yè)的跳轉(zhuǎn):
$scope.$location = $location; //將$location掛載到$scope下,模板中便可使用$location提供的方法
模板如下:
<li ng-repeat="item in data" ng-click="$location.path('detail/'+item.id)"> --點(diǎn)擊的時(shí)候?qū)⒘斜硖D(zhuǎn)到詳情頁(yè)
4、詳情頁(yè)(detail)邏輯分析
m1.controller('detail',['$scope','$resource','$location',function($scope,$resource,$location){
var id = parseInt($location.path().substring(8)); //獲取索引
$resource('infor.json').query(function(data){
$scope.data = data[id];
$scope.links = eval($scope.data.showList); //自定義指令需要用到此數(shù)據(jù)
});
}]);
//注意:$resource.query()為異步操作。數(shù)據(jù)相關(guān)的邏輯需要在回調(diào)中完成,否則可能會(huì)出現(xiàn)數(shù)據(jù)undefined的情況。
5、自定義指定slide的編寫(xiě)
AngularJS的自定義指定功能十分強(qiáng)大,為實(shí)現(xiàn)組件化開(kāi)發(fā)提供了一種思路。下面簡(jiǎn)單地實(shí)現(xiàn)一個(gè)輪播組件。
用法示例: <slide links='links' w='200' h='200'></slide>
模板(slide.html)如下:
<div class="slide">
<ul>
<li ng-repeat="item in links">
<img ng-src={{item.url}}>
</li>
</ul>
</div>
m1.directive('slide',function(){
return {
restrict : 'E',
templateUrl : 'template/slide.html',
replace : true,
scope : {
links : '=',
w : '@',
h : '@'
},
link : function(scope,element,attris){
setTimeout(function(){
var w = scope.links.length * scope.w;
var h = scope.h;
var iNow = 0;
$(element).css({'width':scope.w,'height':h,'position':'relative','overflow':'hidden'})
$(element).find('ul').css({'width':w,'height':h,'position':'absolute'});
setTimeout(function(){
$(element).find('li').css({'width':scope.w,'height':h,'float':'left'});
$(element).find('img').css({'width':scope.w,'height':h});
},0);
setInterval(function(){
iNow++;
$(element).find('ul').animate({'left':-iNow*scope.w},function(){
if(iNow==scope.links.length-1){
$(element).find('ul').css('left',0);
iNow = 0;
}
});
},1000)
},50)
}
}
})
結(jié)語(yǔ)
AngularJS給我們提供了很多好用的功能,比如路由的管理、數(shù)據(jù)的過(guò)濾的等。更強(qiáng)大的功能還需要進(jìn)一步的探索,此文僅作為一個(gè)好的開(kāi)端。
相關(guān)文章
Angular.js指令學(xué)習(xí)中一些重要屬性的用法教程
這篇文章主要給大家介紹了關(guān)于A(yíng)ngular.js指令學(xué)習(xí)中一些重要屬性的用法教程,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
AngularJS directive返回對(duì)象屬性詳解
這篇文章主要為大家纖細(xì)介紹了AngularJS directive返回對(duì)象屬性的相關(guān)內(nèi)容,感興趣的小伙伴們可以參考一下2016-03-03
Angular2中如何使用ngx-translate進(jìn)行國(guó)際化
本篇文章主要介紹了Angular2中使用ngx-translate進(jìn)行國(guó)際化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
AngularJS輔助庫(kù)browserTrigger用法示例
這篇文章主要介紹了AngularJS輔助庫(kù)browserTrigger用法,結(jié)合實(shí)例形式分析了輔助庫(kù)browserTrigger的功能及單元測(cè)試中的使用技巧,需要的朋友可以參考下2016-11-11
angular組件間通訊的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于angular組件間通訊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用angular組件具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
AngularJS自定義表單驗(yàn)證功能實(shí)例詳解
這篇文章主要介紹了AngularJS自定義表單驗(yàn)證功能,結(jié)合完整實(shí)例形式詳細(xì)分析了AngularJS實(shí)現(xiàn)表單驗(yàn)證的相關(guān)指令、模型綁定、數(shù)據(jù)驗(yàn)證等操作技巧,需要的朋友可以參考下2018-08-08
詳解在A(yíng)ngular4中使用ng2-baidu-map的方法
這篇文章主要介紹了在A(yíng)ngular4中使用ng2-baidu-map的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
AngularJS service之select下拉菜單效果
這篇文章主要為大家詳細(xì)介紹了AngularJS service之select下拉菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Angularjs自定義指令實(shí)現(xiàn)三級(jí)聯(lián)動(dòng) 選擇地理位置
這篇文章主要介紹了Angularjs自定義指令實(shí)現(xiàn)三級(jí)聯(lián)動(dòng),選擇地理位置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

