ボタンでの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>
Guysは、あなたのANを書きますしてくださいcart.component.html私は自分が何をすべきか理解していないので、より詳細に言います。
'removeEventListener'に渡される2番目の関数は、関数への参照であり、関数自体ではありません。リスナーを削除する場合は、名前付き関数を使用する必要があります。 – Teemu
@Teemu - 関数は参照によって渡されます。関数に名前を付ける必要はありません。 (「名前を持つ」と「変数によって参照される」を混同しないでください)。毎回同じ機能が必要です。 – Quentin
@クェンティンええ、私の知るところによれば、私のコメントには貧弱な文言があります。 – Teemu