버튼에서 onclick 이벤트를 삭제할 수있는 방법을 찾을 수 없습니다 (여기, 여기, 여기, 여기). 나는이 문제에 대한 해결책을 찾기 위해 많은 시간을 보냈지 만 여전히 그것을 찾지 못했습니다. 나는 누군가가 나를 도울 수 있기를 바랍니다, 감사합니다.'event.srcElement.removeEventListener'가 작동하지 않습니다 - TypeScript
cart.component.ts
getCartProducts(event: Event, cart: ICart) {
for (let product of cart.Products)
{
event.srcElement.parentElement.parentElement.insertAdjacentHTML("afterend", "<tr><td>" + product.Name + "</td><td>" + product.Price + "</td></tr>");
}
event.srcElement.textContent = "Скрыть продукты";
event.srcElement.attributes.getNamedItem("title").value = "Скрыть продукты";
/* this doesn't work!! */ event.srcElement.removeEventListener("click", function (e) { CartsComponent.prototype.getCartProducts(event, cart) }, false);
/* but this works!! */ event.srcElement.addEventListener("click", function (e) { CartsComponent.prototype.hideCartProducts(event) }, false);
}
hideCartProducts(event: Event) {
event.srcElement.parentElement.parentElement.insertAdjacentHTML("afterend", "<span>qwqeq</span>");
}
<table class='table table-striped' *ngIf='carts && carts.length'>
<thead>
<tr>
<th>Название</th>
<th>Описание</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let cart of carts">
<td>{{cart.Name}}</td>
<td>{{cart.Description}}</td>
<td>
<button title="Показать продукты" class="btn btn-show" (click)="getCartProducts($event, cart)">Показать продукты</button> //here here here here here
<button title="Изменить" class="btn btn-primary" (click)="editCart(cart.Id)">Изменить</button>
<button title="Удалить" class="btn btn-danger" (click)="deleteCart(cart.Id)">Удалить</button>
</td>
</tr>
</tbody>
</table>
얘들 아, 당신의 쓰기하겠습니다 cart.component.html 왜냐하면 나는 내가해야 할 일을 이해하지 못하기 때문이다.
'removeEventListener'에 전달 된 두 번째 함수는 funtion 자체가 아닌 함수에 대한 참조 여야합니다. 리스너를 제거하려면 명명 된 함수를 사용해야합니다. – Teemu
@Teemu - 함수는 참조로만 전달됩니다. 함수에 이름을 지정할 필요가 없습니다. ("이름 가진"과 "변수에 의해 참조되는"을 혼동하지 마십시오.) 매번 같은 기능을 수행하면됩니다. – Quentin
@Quentin 그래, 나도 알아, 내 코멘트에 가난한 표현. – Teemu