@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} |
In this step, you will add a clickable phone image swapper to the phone details page.
1. Reset your workspace to Step 10 using:
git checkout --force step-10
or
./goto_step.sh 10
2. Refresh your browser or check the app out on {@link
http://angular.github.com/angular-phonecat/step-10/app our 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 how we can do this with angular.
The most important changes are listed below. You can see the full diff on {@link
https://github.com/angular/angular-phonecat/compare/step-9...step-10
GitHub}:
## Controller
__`app/js/controllers.js`:__
...
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'];
In the `PhoneDetailCtrl` controller, the statement `self.mainImageUrl = response.images[0];`
creates the `mainImageUrl` model property and set its default value to the first phone image url.
We also created a `setImage` controller method to change the value of `mainImageUrl`.
## Template
__`app/partials/phone-detail.html`:__
...
...
We bound the `ng:src` attribute of the large image to the `mainImageUrl` property.
We also registered an `{@link angular.directive.ng:click ng:click}` handler with thumbnail images.
When a user clicks on one of the thumbnail images, the handler will use the `setImage` controller
method to change the value of the `mainImageUrl` property to the url of the thumbnail image.
## 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`:__
...
describe('Phone detail view', function() {
beforeEach(function() {
browser().navigateTo('../../app/index.html#/phones/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');
});
});
});
You can now refresh the browser tab with the end-to-end test runner to see the tests run, or you
can see them running on {@link
http://angular.github.com/angular-phonecat/step-8/test/e2e/runner.html
angular's server}.
With the phone image swapper in place, we're ready for Step 11 (the last step!) to learn an even
better way to fetch data.
| {@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} |