[Angular Tutorial] 12-event handlers

Source: Internet
Author: User

In this step, you will add a clickable phone picture converter to the phone details page.

• The phone Details page shows a large picture of the current phone and several relatively small sketches. It would be nice if we could just click on the thumbnail to change the big picture to a sketch. Let's see how to implement it with angular.

The biggest differences are listed below, you can click here to view all the differences on GitHub.

Component Controller

app/phone-detail/phone-detail.component.js:

Controller: [' $http ', ' $routeParams ',  function Phonedetailcontroller ($http, $routeParams) {    var = this ;    Self.setimage = function SetImage (imageUrl) {      self.mainimageurl = imageUrl;    };    $http. Get (' phones/' + $routeParams. Phoneid + '. JSON '). Then (function (response) {      self.phone = response.data;      Self.setimage (Self.phone.images[0]);}  ] ...

In the Phonedetail component Controller, we created the mainimageurl data model and set its default value to the first picture URL.

We also created the setimage () method (which is treated as the event handler), which alters the value of the mainimageurl .

Component templates

app/phone-detail/phone-detail.template.html:

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

We will ngsrc directive is bound to $ctrl . mainimageurl property.

We also used a sketch to register a Ngclick processor. 。 When the user clicks on one of the sketches, the processor will use $ctrl < Span style= "Background-color: #c0c0c0;" >. setimage () method callback to $ctrl . mainimageurl property is changed to the URL of the selected sketch.

Experiment

• Similar to the ngclick directive, this directive binds the angular expression to the click Event, and there are many instructions built into the native event, such as dblclick focus /blur,鼠标和键事件等等。

Let's add a new controller method to the Phonedetail component Controller.

Self.ondblclick = function OnDblclick (IMAGEURL) {  alert (' You double-clicked Image: ' + imageUrl);};

And inphone-detail.template.html中添加如下元素:

Now, whenever you double-click a sketch, a warning box pops up, and it's annoying!

Summarize

Complete the correct exchange of phone pictures, we will learn a better way to get the data in the next step.

[Angular Tutorial] 12-event handlers

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.