Cod:
export class ViewModel {
public users: knockout.koObservableArrayBase;
constructor () {
this.users = ko.observableArray([]);
this.removeUser = this.removeUser.bind(this);//<-- Here compiller shows error
}
removeUser(user: User): void {
this.users.remove(user);
}
}
hTML:
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
</tr>
</thead>
<tbody data-bind=foreach: users>
<tr>
<td><a href=# data-bind=click: $root.removeUser>Remove</a></td>
<td data-bind=text: name></td>
<td data-bind=text: surname></td>
</tr>
</tbody>
</table>
Problema este în metoda removeUser. În mod implicit, dacă nu se leagă context, acest == UserToDelete - obiect nu viewModel. Dacă am adăugarea la constructor: this.removeUser = this.removeUser.bind(this); (manually enforce context), atunci context , este necesar ca acest viewmodel ==, dar apoi typescript se plânge pentru „Nu se poate converti la funcții. (Utilizator: Utilizator) => void necesită un apel de semnături, dar funcția îi lipsește unul“













