码迷,mamicode.com
首页 > Web开发 > 详细

AngularJS学习---更多模板(More Templating) step 8

时间:2014-05-13 10:27:48      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:android   des   style   blog   class   code   

 

1.切换分支

amosli@amosli-pc:~/develop/angular-phonecat$ git checkout step-8 #切换分支
amosli@amosli-pc:~/develop/angular-phonecat$ npm start #启动项目

2.需求:

将step 7 中的手机详细信息展示出来,加上各种参数配置,图片展示等等.

3.效果:

bubuko.com,布布扣

bubuko.com,布布扣

 

这里很明显要比step 7中的信息详细的多,而且效果要好很多.究竟是怎么实现的呢?

 

3.实现代码:

首先,所有需要展示的图片都是在app/img/phones目录下.

其次,所有手机的参数都是配置在app/phones目录下,都是存放在phones.json文件中.

举例:

DATA(数据):

app/phones/nexus-s.json: (举其中一个手机为例)

{"additionalFeatures":"Contour Display, Near Field Communications (NFC),...","android":{"os":"Android 2.3","ui":"Android"},..."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"],"storage":{"flash":"16384MB","ram":"512MB"}}

Controller(控制器):

app/js/controllers.js:

bubuko.com,布布扣
var phonecatControllers = angular.module(‘phonecatControllers‘, []);

phonecatControllers.controller(‘PhoneListCtrl‘, [‘$scope‘, ‘$http‘,
  function($scope, $http) {
    $http.get(‘phones/phones.json‘).success(function(data) {
      $scope.phones = data;
    });

    $scope.orderProp = ‘age‘;
  }]);

phonecatControllers.controller(‘PhoneDetailCtrl‘, [‘$scope‘, ‘$routeParams‘, ‘$http‘,
  function($scope, $routeParams, $http) {
    $http.get(‘phones/‘ + $routeParams.phoneId + ‘.json‘).success(function(data) {
      $scope.phone = data;
    });
  }]);
bubuko.com,布布扣

这里是控制数据的读取和页面的展示.

app/partials/phone-detail.html:

bubuko.com,布布扣
amosli@amosli-pc:~/develop/angular-phonecat/app$ cat partials/phone-detail.html
<img ng-src="{{phone.images[0]}}" class="phone">

<h1>{{phone.name}}</h1>

<p>{{phone.description}}</p>

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

<ul class="specs">
  <li>
    <span>Availability and Networks</span>
    <dl>
      <dt>Availability</dt>
      <dd ng-repeat="availability in phone.availability">{{availability}}</dd>
    </dl>
  </li>
 .....
 .....
</ul>
bubuko.com,布布扣

 

这里主要注意在显示数据时,模板中对于图片url的遍历以及展示用的是<ling-repeat="img in phone.images"><imgng-src="{{img}}"></li>

如果要展示指定的数组中的图片可以在phone.images[i],i表示下标,从0开始.

 

4.测试(TEST)

test/unit/controllersSpec.js:

bubuko.com,布布扣
  beforeEach(module(‘phonecatApp‘));

  ...

  describe(‘PhoneDetailCtrl‘, function(){
    var scope, $httpBackend, ctrl;

    beforeEach(inject(function(_$httpBackend_, $rootScope, $routeParams, $controller) {
      $httpBackend = _$httpBackend_;
      $httpBackend.expectGET(‘phones/xyz.json‘).respond({name:‘phone xyz‘});

      $routeParams.phoneId = ‘xyz‘;
      scope = $rootScope.$new();
      ctrl = $controller(‘PhoneDetailCtrl‘, {$scope: scope});
    }));


    it(‘should fetch phone detail‘, function() {
      expect(scope.phone).toBeUndefined();
      $httpBackend.flush();

      expect(scope.phone).toEqual({name:‘phone xyz‘});
    });
  });
...
bubuko.com,布布扣

启动测试:

bubuko.com,布布扣
amosli@amosli-pc:~/develop/angular-phonecat$ npm run protractor 

#测试结果
....
Using ChromeDriver directly...
.....

Finished in 8.325 seconds
5 tests, 8 assertions, 0 failures
bubuko.com,布布扣

 

 

 

AngularJS学习---更多模板(More Templating) step 8,布布扣,bubuko.com

AngularJS学习---更多模板(More Templating) step 8

标签:android   des   style   blog   class   code   

原文地址:http://www.cnblogs.com/amosli/p/3724618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!