@workInProgress @ngdoc overview @name Tutorial: Step 10 @description
| {@link tutorial.step_09 Previous} | {@link http://angular.github.com/angular-phonecat/step-10/app Live Demo } | {@link tutorial Tutorial Home} | {@link https://github.com/angular/angular-phonecat/compare/step-9...step-10 Code Diff} | {@link tutorial.step_11 Next} |
![]()
{{phone.name}}
{{phone.description}}
...
function PhoneDetailCtrl($xhr) {
var self = this;
$xhr('GET', 'phones/' + self.params.phoneId + '.json', function(code, response) {
self.phone = response;
self.mainImageUrl = response.images[0];
});
self.setImage = function(imageUrl) {
self.mainImageUrl = imageUrl;
}
}
//PhoneDetailCtrl.$inject = ['$xhr'];
__`test/e2e/scenarios.js`:__
/* jasmine-like end2end tests go here */
...
describe('Phone detail view', function() {
beforeEach(function() {
browser().navigateTo('../../app/index.html#/phones/nexus-s');
});
it('should display nexus-s page', function() {
expect(binding('phone.name')).toBe('Nexus S');
});
it('should display the first phone image as the main phone image', function() {
expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.0.jpg');
});
it('should swap main image if a thumbnail image is clicked on', function() {
element('.phone-thumbs li:nth-child(3) img').click();
expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.2.jpg');
element('.phone-thumbs li:nth-child(1) img').click();
expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.0.jpg');
});
});
});
## Discussion:
Adding the phone image swapping feature is fairly straightforward:
* We defined the `mainImageUrl` model property in the details controller (`PhoneDetailCtrl`) and
set the default value of `mainImageUrl` to the first image in the array of images.
* We created a `setImage` controller method to change `mainImageUrl` to the image clicked on by
the user.
* We registered an `{@link angular.directive.ng:click ng:click}` handler for thumb images to use
the `setImage` controller method.
* We expanded the end-to-end test to verify that our new feature is swapping images correctly.
| {@link tutorial.step_09 Previous} | {@link http://angular.github.com/angular-phonecat/step-10/app Live Demo } | {@link tutorial Tutorial Home} | {@link https://github.com/angular/angular-phonecat/compare/step-9...step-10 Code Diff} | {@link tutorial.step_11 Next} |