它正在工作,请参阅我的 plunker:https ://plnkr.co/edit/ZIjepnYZ5IS8FfktU0C1?p=preview
您需要将它们推images[i]
送到数组中,而不是每次都分配它。
import {Component, NgModule, Input} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-cmp',
template: `my-cmp!`,
})
export class MyCmp {
private _images: any[];
private _threeImages: any[];
@Input() set images(images: any[]) {
this._images = images;
this._threeImages = []; // CLEAR IT !
for(let i=0; i < 3; i++) {
this._threeImages.push(images[i]);
}
console.log(this._images);
console.log(this._threeImages);
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
<my-cmp [images]="myImages"></my-cmp>
`,
})
export class App {
private myImages: any[] = [
{},
{},
{},
{},
{}
];
constructor() {
this.name = 'Angular2'
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, MyCmp ],
bootstrap: [ App ]
})
export class AppModule {}