我有 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);
}
}
当click
被触发时,我将选择的命令传递给add
a 的方法CommandLineComponent
:
export class CommandLineComponent {
commands: string[] = [];
add(command: string): void {
if (command) this.commands.push(command);
console.log(this.commands);
}
}
在一个CommandLineComponent
i 的模板中,使用 *ngFor 打印一个命令列表:
<li *ngFor="#command of commands" class="b-command-textarea__command">{{command}}</li>
commands
但是当我选择更新的命令和数组时, *ngFor 不会触发CommandLineComponent
。因此,数据绑定不起作用。commands
阵列更新成功:
谢谢你的帮助。