我不明白为什么 Angular 在不使用 @Output EventEmitter 的情况下更新组件内部修改的一个属性 this.list
,我注意到只有在我传递数组时才会发生这种情况。
import { Component,OnInit } from "@angular/core";
import { Item } from "./models/item";
@Component({
selector: "app-root",templateUrl: "./app.component.html",styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
title = "Hello";
list: Item[];
constructor() {
this.list = [
{
text: "Link 1"
},{
text: "Link 2"
}
];
setTimeout(() => {
console.log("Itens",this.list);
},11500);
}
ngOnInit() {}
}
注意我没有使用双向绑定 [()]
<menu [items]="list"></menu>
组件:
import { Component,Input } from "@angular/core";
import { Item } from "../../models/item";
@Component({
selector: "menu",styleUrls: ["./menu.component.css"],templateUrl: "./menu.component.html"
})
export class MenuComponent {
@Input() items: Item[];
constructor() {
// after 10 seconds change the data
setTimeout(() => {
this.items.push({
text: "Link 3"
});
},5000);
}
}