角度变化检测更新 ngfor 中的所有组件
angular4 change detection update all components inside ngfor
我有 2 个组成部分:
x-list-component 和 xcomponent.
x-list-component 使用 *ngfor='let VAR of array' 指令创建 xcomponent 列表并将 VAR 作为 xcomponent 的输入。
<xcomponent [VAR]='VAR'></xcomponent>
xcomponent 使用模板中的插值在 div 中呈现传递的 VAR。
{{VAR.args}}
一切正常,但是当在 xcomponent 的一个实例中发生 changedetection 时,视图被重新渲染并且 VAR.args 的新值出现在所有其他 xcomponents 而不是仅出现在修改了他的 xcomponent VAR.args.
所以我想知道如何只更新发出变化检测的 xcomponent 的视图并保持其他 xcomponents 的状态不变。
//list component and his template
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-kameroke-list',
templateUrl: './kameroke-list.component.html',
styleUrls: ['./kameroke-list.component.css']
})
export class KamerokeListComponent implements OnInit {
lyrics=[{"first":"paroles","second":"paroles2","third":"paroles3","id":"test"},
{"first":"lyrics","second":"lyrics2","third":"lyrics3","id":"secondTest"}
];
constructor() { }
ngOnInit() {
}
}
<!--template for list-component-->
<app-kameroke *ngFor="let lyric of lyrics" [first]="lyric.first" [second]="lyric.second" [third]="lyric.third"></app-kameroke>
<!--attribuer un id dynamiquement : [attr.id]="lyric.id"-->
//kamerokeComponent and his template
import { Component, OnInit , Input ,ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-kameroke',
templateUrl: './kameroke.component.html',
styleUrls: ['./kameroke.component.css']
})
export class KamerokeComponent implements OnInit {
@Input() url:string;
@Input() private first:string;
@Input() private second:string;
@Input() private third:string;
masked:boolean=true;
playing:boolean=false;
widget :any;
lyrics:object;
constructor(private ref:ChangeDetectorRef) {
}
ngOnInit() {
//all this event in insertFrame
this.lyrics={
1:"ca marche",
2:"ca cartonne",
3:"c le feu",
4:"testons voir"
};
this.widget=window['SC'].Widget(document.getElementById("test"));
this.widget.bind(window['SC'].Widget.Events.PLAY,()=>{this.playing=true});
this.widget.bind(window['SC'].Widget.Events.PAUSE,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.FINISH,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.PLAY_PROGRESS,()=>{
this.widget.getPosition(
(time)=>{
//TODO set les variables first second and third
if(this.lyrics){
let timing=Math.ceil(time/1000);
//console.log(this.lyrics[timing]);
if(this.lyrics[timing]){
console.log(timing);
this.first=this.lyrics[timing];
this.second=this.lyrics[timing];
this.third=this.lyrics[timing];
this.ref.detectChanges();
}
}
}
);
});
}
hide(){
this.masked=false;
}
}
<!--kamerokeComponent template-->
<div class="container">
<div class="row">
<div class="col-sm soundcloudCol">
<iframe (load)="hide()" id="test" allow="autoplay" width="100%" height="100%" scrolling="no" frameborder="no"
src="https://w.soundcloud.com/player/?url=https://soundcloud.com/maahlox/la-vie-cest-la-bastonde&{ ADD YOUR PARAMETERS HERE }">
</iframe>
</div>
</div>
<div class="row lyrics" [hidden]="masked" >
<div class="col-sm">
<br><br>
<p class="first">{{first}}</p>
<p class="second">{{second}}</p>
<p class="second">{{third}}</p>
</div>
</div>
</div>
这是因为在您的 ngOnInit
上,您通过 id 从硬编码值 "test"
获取元素。那么对于所有 kamerokeComponent
组件,将只有一个小部件元素。因此,当您更改时,所有 kamerokeComponent
值都将更改。我建议也传递元素的 id
。
// template for list-component
<app-kameroke *ngFor="let lyric of lyrics" [first]="lyric.first"
[second]="lyric.second" [third]="lyric.third" [id]="lyric.id"></app-kameroke>
//kamerokeComponent
import { Component, OnInit , Input ,ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-kameroke',
templateUrl: './kameroke.component.html',
styleUrls: ['./kameroke.component.css']
})
export class KamerokeComponent {
@Input() url:string;
@Input() private first:string;
@Input() private second:string;
@Input() private third:string;
@Input() private id:string;
masked:boolean=true;
playing:boolean=false;
widget :any;
lyrics:object;
constructor(private ref:ChangeDetectorRef) {
}
ngAfterViewInit() {
//all this event in insertFrame
this.lyrics={
1:"ca marche",
2:"ca cartonne",
3:"c le feu",
4:"testons voir"
};
this.widget=window['SC'].Widget(document.getElementById(this.id));
this.widget.bind(window['SC'].Widget.Events.PLAY,()=>{this.playing=true});
this.widget.bind(window['SC'].Widget.Events.PAUSE,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.FINISH,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.PLAY_PROGRESS,()=>{
this.widget.getPosition((time)=>{
//TODO set les variables first second and third
if(this.lyrics){
let timing=Math.ceil(time/1000);
//console.log(this.lyrics[timing]);
if(this.lyrics[timing]){
console.log(timing);
this.first=this.lyrics[timing];
this.second=this.lyrics[timing];
this.third=this.lyrics[timing];
this.ref.detectChanges();
}
}
});
});
}
hide(){
this.masked=false;
}
}
我有 2 个组成部分:
x-list-component 和 xcomponent.
x-list-component 使用 *ngfor='let VAR of array' 指令创建 xcomponent 列表并将 VAR 作为 xcomponent 的输入。
<xcomponent [VAR]='VAR'></xcomponent>
xcomponent 使用模板中的插值在 div 中呈现传递的 VAR。 {{VAR.args}}
一切正常,但是当在 xcomponent 的一个实例中发生 changedetection 时,视图被重新渲染并且 VAR.args 的新值出现在所有其他 xcomponents 而不是仅出现在修改了他的 xcomponent VAR.args.
所以我想知道如何只更新发出变化检测的 xcomponent 的视图并保持其他 xcomponents 的状态不变。
//list component and his template
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-kameroke-list',
templateUrl: './kameroke-list.component.html',
styleUrls: ['./kameroke-list.component.css']
})
export class KamerokeListComponent implements OnInit {
lyrics=[{"first":"paroles","second":"paroles2","third":"paroles3","id":"test"},
{"first":"lyrics","second":"lyrics2","third":"lyrics3","id":"secondTest"}
];
constructor() { }
ngOnInit() {
}
}
<!--template for list-component-->
<app-kameroke *ngFor="let lyric of lyrics" [first]="lyric.first" [second]="lyric.second" [third]="lyric.third"></app-kameroke>
<!--attribuer un id dynamiquement : [attr.id]="lyric.id"-->
//kamerokeComponent and his template
import { Component, OnInit , Input ,ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-kameroke',
templateUrl: './kameroke.component.html',
styleUrls: ['./kameroke.component.css']
})
export class KamerokeComponent implements OnInit {
@Input() url:string;
@Input() private first:string;
@Input() private second:string;
@Input() private third:string;
masked:boolean=true;
playing:boolean=false;
widget :any;
lyrics:object;
constructor(private ref:ChangeDetectorRef) {
}
ngOnInit() {
//all this event in insertFrame
this.lyrics={
1:"ca marche",
2:"ca cartonne",
3:"c le feu",
4:"testons voir"
};
this.widget=window['SC'].Widget(document.getElementById("test"));
this.widget.bind(window['SC'].Widget.Events.PLAY,()=>{this.playing=true});
this.widget.bind(window['SC'].Widget.Events.PAUSE,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.FINISH,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.PLAY_PROGRESS,()=>{
this.widget.getPosition(
(time)=>{
//TODO set les variables first second and third
if(this.lyrics){
let timing=Math.ceil(time/1000);
//console.log(this.lyrics[timing]);
if(this.lyrics[timing]){
console.log(timing);
this.first=this.lyrics[timing];
this.second=this.lyrics[timing];
this.third=this.lyrics[timing];
this.ref.detectChanges();
}
}
}
);
});
}
hide(){
this.masked=false;
}
}
<!--kamerokeComponent template-->
<div class="container">
<div class="row">
<div class="col-sm soundcloudCol">
<iframe (load)="hide()" id="test" allow="autoplay" width="100%" height="100%" scrolling="no" frameborder="no"
src="https://w.soundcloud.com/player/?url=https://soundcloud.com/maahlox/la-vie-cest-la-bastonde&{ ADD YOUR PARAMETERS HERE }">
</iframe>
</div>
</div>
<div class="row lyrics" [hidden]="masked" >
<div class="col-sm">
<br><br>
<p class="first">{{first}}</p>
<p class="second">{{second}}</p>
<p class="second">{{third}}</p>
</div>
</div>
</div>
这是因为在您的 ngOnInit
上,您通过 id 从硬编码值 "test"
获取元素。那么对于所有 kamerokeComponent
组件,将只有一个小部件元素。因此,当您更改时,所有 kamerokeComponent
值都将更改。我建议也传递元素的 id
。
// template for list-component
<app-kameroke *ngFor="let lyric of lyrics" [first]="lyric.first"
[second]="lyric.second" [third]="lyric.third" [id]="lyric.id"></app-kameroke>
//kamerokeComponent
import { Component, OnInit , Input ,ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-kameroke',
templateUrl: './kameroke.component.html',
styleUrls: ['./kameroke.component.css']
})
export class KamerokeComponent {
@Input() url:string;
@Input() private first:string;
@Input() private second:string;
@Input() private third:string;
@Input() private id:string;
masked:boolean=true;
playing:boolean=false;
widget :any;
lyrics:object;
constructor(private ref:ChangeDetectorRef) {
}
ngAfterViewInit() {
//all this event in insertFrame
this.lyrics={
1:"ca marche",
2:"ca cartonne",
3:"c le feu",
4:"testons voir"
};
this.widget=window['SC'].Widget(document.getElementById(this.id));
this.widget.bind(window['SC'].Widget.Events.PLAY,()=>{this.playing=true});
this.widget.bind(window['SC'].Widget.Events.PAUSE,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.FINISH,()=>{this.playing=false});
this.widget.bind(window['SC'].Widget.Events.PLAY_PROGRESS,()=>{
this.widget.getPosition((time)=>{
//TODO set les variables first second and third
if(this.lyrics){
let timing=Math.ceil(time/1000);
//console.log(this.lyrics[timing]);
if(this.lyrics[timing]){
console.log(timing);
this.first=this.lyrics[timing];
this.second=this.lyrics[timing];
this.third=this.lyrics[timing];
this.ref.detectChanges();
}
}
});
});
}
hide(){
this.masked=false;
}
}