Improve this doc

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

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 of mainImageUrl.

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 the setImage event handler function to change the value of the mainImageUrl property to the URL of the thumbnail image.

TODO! Experiments - 图1

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 on Angular's server.

Experiments

  • Let's add a new controller method to PhoneDetailCtrl:
  1. $scope.hello = function(name) {
  2. alert('Hello ' + (name || 'world') + '!');
  3. }

and add:

  1. <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.