Ich habe 2 Komponenten: CommandListComponent
und CommandLineComponent
. Innerhalb einer CommandListComponent
Vorlage behandeln i ein Click-Ereignis auf einer Textzeichenfolge:ngFor feuert nicht nach Update abhängig von der Variablen in Angular2
CommandListComponent
Vorlage:
<li *ngFor="#command of commandList" class="b-command-list__command"><span (click)="checkCommand(command)" class="b-command-list__text">{{command}}</span></li>
commandlist.component.ts
import {CommandLineComponent} from "./commandline.component";
...
export class CommandListComponent {
commandLineComponent: any;
constructor(private _commandLine: CommandLineComponent) {
this.commandLineComponent = _commandLine;
}
checkCommand(command: string): void {
this.commandLineComponent.add(command);
}
}
Wenn click
abgefeuert wird passieren i gewählte Befehl add
Methode eines CommandLineComponent
:
export class CommandLineComponent {
commands: string[] = [];
add(command: string): void {
if (command) this.commands.push(command);
console.log(this.commands);
}
}
Und innerhalb einer Vorlage eines CommandLineComponent
i eine Liste eines Kommandos drucken mit * ngFor:
<li *ngFor="#command of commands" class="b-command-textarea__command">{{command}}</li>
Aber * ngFor wird nicht ausgelöst, wenn ich einen Befehl und commands
Array von CommandLineComponent
aktualisiert. Also, Datenbindung funktioniert nicht. commands
Array aktualisiert erfolgreich:
Sie um Hilfe danken.
ich ein 'CommandLineService' erstellen und Befehle an sie senden. Es funktioniert großartig. Aber Subskriptionen in 'CommandLineComponent' werden nicht ausgelöst: [code snippet] (http://data3.floomby.com/files/share/6_5_2016/15/rLPNLhsRtEiW0coFxJ0DpA.jpg) – Edward
Wie lösen Sie das Ereignis aus:' this.commandAdded .next (Befehl); '? Definieren Sie den 'CommandLineService' beim Bootstrapping Ihrer Anwendung? –
Ich vermute, dass Sie nicht die gleiche Instanz teilen ... –