Event delegate for Angular

Source: Internet
Author: User

Event delegates: If the child elements are constantly changing and the number is numerous, you need to repeatedly bind events and waste resources. You can then bind the event to the parent element and then find the event object that triggered the event through Event.target. Then get the data you need to execute the binding event.

In angular, the event object cannot be obtained directly through Event.target, but must be obtained by passing the parameter $event and then adding a parameter to the event that is bound in the component in the following code:

In HTML:

<div class= "Replytext" ng-click= "DeleteText ($event)">
<p ng-repeat= "Val in discuss" >
<span><b>{{val.name}}: </b></span>
<span>{{val.text}}</span>
<span class= "Time" >{{val.time}} <a href= "javascript:;" id={{val._id}}> Delete </a></span>
</p>
</div>

JS in:

$scope. DeleteText = function ($event) {
var id = $event. target.id;
Console.info (ID);
var bool = confirm ("Really want to delete?"). ");
if (bool) {
$http. Delete ("/delete/reply/text/" + ID). Success (function (data) {
if (data.tips) {
GetDate ();
}
else{
Alert ("Delete failed");
}
});
}
};

Event delegate for Angular

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.