と の 2 つのコンポーネントがCommandListComponent
ありCommandLineComponent
ます。CommandListComponent
テンプレート内で、テキスト文字列のクリック イベントを処理します。
CommandListComponent
テンプレート:
<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);
}
}
が起動すると、選択したコマンドをa のメソッドにclick
渡します。add
CommandLineComponent
export class CommandLineComponent {
commands: string[] = [];
add(command: string): void {
if (command) this.commands.push(command);
console.log(this.commands);
}
}
そして、i のテンプレート内で、CommandLineComponent
*ngFor を使用してコマンドのリストを出力します。
<li *ngFor="#command of commands" class="b-command-textarea__command">{{command}}</li>
commands
しかし、更新されたコマンドと配列を選択しても *ngFor は起動しませんCommandLineComponent
。そのため、データバインディングは機能していません。commands
アレイは正常に更新されました:
ご協力ありがとう御座います。