Angularjs the event handler for the Getting Started tutorial _angularjs

Source: Internet
Author: User

In this step, you will be on the phone details page so that the phone picture can be clicked.

Please reset the working directory:

Git checkout-f step-10

The phone Details view shows a large picture of the current phone and a few smaller thumbnails. It would be better if the user clicked on the thumbnail to replace the big one. Now let's see how to implement it with ANGULARJS.

The most important differences between steps 9 and 10 are listed below. You can see the whole difference in the GitHub.

Controller

App/js/controllers.js

...
function Phonedetailctrl ($scope, $routeParams, $http) {
 $http. Get (' phones/' + $routeParams. Phoneid + '. Json '). Success (function (data) {
 $scope. phone = data;
 $scope. Mainimageurl = Data.images[0];
 });

 $scope. setimage = function (IMAGEURL) {
 $scope. mainimageurl = ImageUrl;
 }
}

Phonedetailctrl $inject = [' $scope ', ' $routeParams ', ' $http '];

In the Phonedetailctrl controller, we created the Mainimageurl model property and set its default value to the URL of the first phone picture.

Template

App/partials/phone-detail.html

 ...

<ul class= "Phone-thumbs" >
 <li ng-repeat= "img in phone.images" >
 
 </li>
</ul>
...

We bind the ngsrc instruction of the large picture to the Mainimageurl attribute.

At the same time we register a ngclick processor to the thumbnail. When a user clicks on any one of the thumbnails, the processor uses the SetImage event handler to set the Mainimageurl property to the URL of the selected thumbnail.

Test

To verify this new feature, we have added two end-to-end tests. A validation master picture is set to the first phone picture by default. The second test clicks on several thumbnails and verifies that the main picture changes as appropriate.

Test/e2e/scenarios.js

...
 Describe (' Phone detail View ', function () {

...

 ) It (' should display the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the main phone image ', function () {
  expect (' 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) IM G '). 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 your browser and run the End-to-end test again, or you can run it on a ANGULARJS server.

Practice

Add a new controller method for Phonedetailctrl:

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

and add:

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

To the phone-details.html template.

Summarize

Now that the picture browser is ready, we are already in step 11 (the last step!). Ready, we'll learn to get the data in a more elegant way.

The above is the Angularjs event processor data collation, follow-up continue to add, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.