angularjs 教程9

In this step, you will implement the phone details view, which is displayed when a user clicks on a phone in the phone list.

  1. Reset the workspace to step 8.

    git checkout -f step-8
  2. Refresh your browser or check the app out on Angular's server.

  1. Reset the workspace to step 8.

    git checkout -f step-8
  2. Refresh your browser or check the app out on Angular's server.

Now when you click on a phone on the list, the phone details page with phone-specific information is displayed.

To implement the phone details view we will use $http to fetch our data, and we'll flesh out the phone-detail.html view template.

The most important changes are listed below. You can see the full diff on GitHub:

Data

In addition to phones.json, the app/phones/ directory also contains one json file for each phone:

app/phones/nexus-s.json: (sample snippet)

 
 
  1. {
  2. "additionalFeatures": "Contour Display, Near Field Communications (NFC),...",
  3. "android": {
  4. "os": "Android 2.3",
  5. "ui": "Android"
  6. },
  7. ...
  8. "images": [
  9. "img/phones/nexus-s.0.jpg",
  10. "img/phones/nexus-s.1.jpg",
  11. "img/phones/nexus-s.2.jpg",
  12. "img/phones/nexus-s.3.jpg"
  13. ],
  14. "storage": {
  15. "flash": "16384MB",
  16. "ram": "512MB"
  17. }
  18. }

Each of these files describes various properties of the phone using the same data structure. We'll show this data in the phone detail view.

Controller

We'll expand the PhoneDetailCtrl by using the $http service to fetch the json files. This works the same way as the phone list controller.

app/js/controllers.js:

 
 
  1. var phonecatControllers = angular.module('phonecatControllers',[]);
  2.  
  3. phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', '$http',
  4. function($scope, $routeParams, $http) {
  5. $http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) {
  6. $scope.phone = data;
  7. });
  8. }]);

To construct the URL for the HTTP request, we use $routeParams.phoneId extracted from the current route by the$route service.

Template

The TBD placeholder line has been replaced with lists and bindings that comprise the phone details. Note where we use the angular{{expression}} markup and ngRepeat to project phone data from our model into the view.

app/partials/phone-detail.html:

 
 
  1. <img ng-src="{{phone.images[0]}}" class="phone">
  2.  
  3. <h1>{{phone.name}}</h1>
  4.  
  5. <p>{{phone.description}}</p>
  6.  
  7. <ul class="phone-thumbs">
  8. <li ng-repeat="img in phone.images">
  9. <img ng-src="{{img}}">
  10. </li>
  11. </ul>
  12.  
  13. <ul class="specs">
  14. <li>
  15. <span>Availability and Networks</span>
  16. <dl>
  17. <dt>Availability</dt>
  18. <dd ng-repeat="availability in phone.availability">{{availability}}</dd>
  19. </dl>
  20. </li>
  21. ...
  22. </li>
  23. <span>Additional Features</span>
  24. <dd>{{phone.additionalFeatures}}</dd>
  25. </li>
  26. </ul>
TODO!

Test

We wrote a new unit test that is similar to the one we wrote for the PhoneListCtrl controller in step 5.

test/unit/controllersSpec.js:

 
 
  1. ...
  2. describe('PhoneDetailCtrl', function(){
  3. var scope, $httpBackend, ctrl;
  4.  
  5. beforeEach(inject(function(_$httpBackend_, $rootScope, $routeParams, $controller) {
  6. $httpBackend = _$httpBackend_;
  7. $httpBackend.expectGET('phones/xyz.json').respond({name:'phone xyz'});
  8.  
  9. $routeParams.phoneId = 'xyz';
  10. scope = $rootScope.$new();
  11. ctrl = $controller('PhoneDetailCtrl', {$scope: scope});
  12. }));
  13.  
  14.  
  15. it('should fetch phone detail', function() {
  16. expect(scope.phone).toBeUndefined();
  17. $httpBackend.flush();
  18.  
  19. expect(scope.phone).toEqual({name:'phone xyz'});
  20. });
  21. });
  22. ...

You should now see the following output in the Karma tab:

Chrome 22.0: Executed 3 of 3 SUCCESS (0.039 secs / 0.012 secs)

We also added a new end-to-end test that navigates to the Nexus S detail page and verifies that the heading on the page is "Nexus S".

test/e2e/scenarios.js:

 
 
  1. ...
  2. describe('Phone detail view', function() {
  3.  
  4. beforeEach(function() {
  5. browser().navigateTo('../../app/index.html#/phones/nexus-s');
  6. });
  7.  
  8.  
  9. it('should display nexus-s page', function() {
  10. expect(binding('phone.name')).toBe('Nexus S');
  11. });
  12. });
  13. ...

You can now rerun ./scripts/e2e-test.sh or refresh the browser tab with the end-to-end test runner to see the tests run, or you can see them running onAngular's server.

Experiments

Summary

Now that the phone details view is in place, proceed to step 9 to learn how to write your own custom display filter.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值