angularjs 教程11

In this step, you will add a clickable phone image swapper to the phone details page.

  1. Reset the workspace to step 10.

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

  1. Reset the workspace to step 10.

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

The phone details view displays one large image of the current phone and several smaller thumbnail images. It would be great if we could replace the large image with any of the thumbnails just by clicking on the desired thumbnail image. Let's have a look at how we can do this with Angular.

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

Controller

app/js/controllers.js:

 
 
  1. ...
  2. var phonecatControllers = angular.module('phonecatControllers',[]);
  3.  
  4. phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', '$http',
  5. function($scope, $routeParams, $http) {
  6. $http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) {
  7. $scope.phone = data;
  8. $scope.mainImageUrl = data.images[0];
  9. });
  10.  
  11. $scope.setImage = function(imageUrl) {
  12. $scope.mainImageUrl = imageUrl;
  13. }
  14. }]);

In the PhoneDetailCtrl controller, we created the mainImageUrl model property and set its default value to the first phone image URL.

We also created a setImage event handler function that will change the value ofmainImageUrl.

Template

app/partials/phone-detail.html:

 
 
  1. <img ng-src="{{mainImageUrl}}" class="phone">
  2.  
  3. ...
  4.  
  5. <ul class="phone-thumbs">
  6. <li ng-repeat="img in phone.images">
  7. <img ng-src="{{img}}" ng-click="setImage(img)">
  8. </li>
  9. </ul>
  10. ...

We bound the ngSrc directive of the large image to the mainImageUrl property.

We also registered an ngClick handler with thumbnail images. When a user clicks on one of the thumbnail images, the handler will use thesetImage event handler function to change the value of the mainImageUrl property to the URL of the thumbnail image.

TODO!

Test

To verify this new feature, we added two end-to-end tests. One verifies that the main image is set to the first phone image by default. The second test clicks on several thumbnail images and verifies that the main image changed appropriately.

test/e2e/scenarios.js:

 
 
  1. ...
  2. describe('Phone detail view', function() {
  3.  
  4. ...
  5.  
  6. it('should display the first phone image as the main phone image', function() {
  7. expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.0.jpg');
  8. });
  9.  
  10.  
  11. it('should swap main image if a thumbnail image is clicked on', function() {
  12. element('.phone-thumbs li:nth-child(3) img').click();
  13. expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.2.jpg');
  14.  
  15. element('.phone-thumbs li:nth-child(1) img').click();
  16. expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.0.jpg');
  17. });
  18. });
  19. });

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

  • Let's add a new controller method to PhoneDetailCtrl:

        $scope.hello = function(name) {
            alert('Hello ' + (name || 'world') + '!');
        }

    and add:

        <button ng-click="hello('Elmo')">Hello</button>

    to the phone-details.html template.

TODO! The controller methods are inherited between controllers/scopes, so you can use the same snippet in the phone-list.html template as well. * Move the hello method from PhoneCatCtrl to PhoneListCtrl and you'll see that the button declared in index.html will stop working, while the one declared in the phone-list.html template remains operational.

Summary

With the phone image swapper in place, we're ready for step 11 to learn an even better way to fetch data.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值