解析angular在github中step-12 利用事件實現縮圖轉場效果,githubstep-12

來源:互聯網
上載者:User

解析angular在github中step-12 利用事件實現縮圖轉場效果,githubstep-12

  

      深入解析angular中事件處理機制中實現縮圖轉場效果 step-12 

一、首先看頁面效果



在頁面中我們點擊右側的小縮圖,左側的圖片能夠做出動態轉場效果,那麼這是怎麼實現的恩?


二、實現思路:

   1、匯入angular-animate.js

   2、在手機詳細視圖我們把大圖片的ngSrc指令綁定到mainImageUrl屬性上,同時我們註冊一個ngClick處理器到縮圖上。當一個使用者點擊縮圖的任意一個時,這個處理器會使用setImage事件處理函數來把mainImageUrl屬性設定成選定縮圖的URL。


三、實現過程分析:

    1、 phone-list.html頁面

    當該頁面載入的時候會調用Phone.query(); 然後$resource會去phones/phones.json檔案。因為在 params:{phoneId:'phones'}中phoneId被賦值為phones。

    如是說:service.js

<span style="font-size:18px;">'use strict';/* Services */var phonecatServices = angular.module('phonecatServices', ['ngResource']);phonecatServices.factory('Phone', ['$resource',function($resource){    return $resource('phones/:phoneId.json', {}, {      query: {method:'GET', params:{phoneId:'phones'}, isArray:true}    });  }]);</span>

   簡要說明如下:

         模組名為:phonecatServices

         服務名:Phone

         定義的方法名為:query、發送的事get請求並傳遞一個參數,變數名為phoneId,返回的是一個數組。



    2、接著我們看下phone-list.html

     

 <ul class="phones">        <li ng-repeat="phone in phones | filter:query | orderBy:orderProp" class="thumbnail phone-listing">          <a href="#/phones/{{phone.id}}" class="thumb"><img ng-src="{{phone.imageUrl}}"></a>          <a href="#/phones/{{phone.id}}">{{phone.name}}</a>          <p>{{phone.snippet}}</p>        </li>      </ul>

    在這裡的每張顯示的圖片都加了一個url,並且每張圖片上都帶有phone.id 這個很重要,因為它攜帶的是json檔案的檔案名稱:比如說phones.json檔案(也就是列表頁面要載入的檔案)

   部分樣本:

 {        "age": 6,         "carrier": "Best Buy",         "id": "nexus-s",         "imageUrl": "img/phones/nexus-s.0.jpg",         "name": "Nexus S",         "snippet": "Fast just got faster with Nexus S. A pure Google experience, Nexus S is the first phone to run Gingerbread (Android 2.3), the fastest version of Android yet."    }, 

   看到沒?這裡的phone.id就是指nexus-s(在下面可以據此找對應的檔案)

  

   當我們點擊圖片時首先執行的是:app.js

'use strict';/* App Module */var phonecatApp = angular.module('phonecatApp', [  'ngRoute', //添加依賴的資源,必須要有angular-route.js  'phonecatAnimations',  'phonecatControllers',  'phonecatFilters',  'phonecatServices']);//定義路由規則phonecatApp.config(['$routeProvider',function($routeProvider) {    $routeProvider.when('/phones', {        templateUrl: 'partials/phone-list.html',        controller: 'PhoneListCtrl'      }).      when('/phones/:phoneId', {         templateUrl: 'partials/phone-detail.html',        controller: 'PhoneDetailCtrl'      }).      otherwise({        redirectTo: '/phones'      });  }]);

     在這裡因為定義了路由規則,自然就到了詳細列表頁面。注意:這裡定義的變數是phoneId ,也就是前一個也面攜帶過來的參數:phone.id 。到了這個頁面就要收PhoneDetailContrl控制了。


    接著再看頁面phone-detail.html

<ul class="phone-thumbs">  <li ng-repeat="img in phone.images">    <img ng-src="{{img}}" ng-click="setImage(img)">  </li></ul>

  小圖是從這裡遍曆出來的,你可能好奇這個phone.images是從哪裡冒出來的?別急。。。


  我們看下控制器部分:

phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 'Phone',  function($scope, $routeParams, Phone) {    $scope.phone = Phone.get({phoneId: $routeParams.phoneId}, function(phone) {      $scope.mainImageUrl = phone.images[0];    });    $scope.setImage = function(imageUrl) {      $scope.mainImageUrl = imageUrl;    }  }]);
   

    通過Phone這個服務發送的get請求根據頁面傳來的phoneId (也即是上面的nexus-s)能夠擷取到相對應的手機介紹資訊,也即是一個手機對象。比如說:nexus-s.json檔案

   部分展示:

"id": "nexus-s",     "images": [        "img/phones/nexus-s.0.jpg",         "img/phones/nexus-s.1.jpg",         "img/phones/nexus-s.2.jpg",         "img/phones/nexus-s.3.jpg"    ], 

    這個時候不就可以使用phone.images來遍曆images 了嗎啊?


   當然今天 的重點是事件響應機制的分析,前面已經做足了鋪墊,看上面的phone-detail.html中是不是有個ng-click指令?當我點擊小圖片的時候首先執行的事setImage函數將當前選中的imgeUrl賦值給主mainImageUrl ,這時候就實現了大圖替換,大圖中預設是顯示json檔案中配置的第一個圖片。

  

         分享是一種美德,學習進步離不開交流 O(∩_∩)O~

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.