英文:
angular - send data to child component
问题
I have a parent component, and three child components.
我有一个父组件和三个子组件。
I send data in these three components, and I use setTimeout to control the time for sending the data.
我在这三个组件中发送数据,并使用setTimeout来控制发送数据的时间。
The first component lasts 5000 milliseconds.
第一个组件持续5000毫秒。
The second component lasts 10000 milliseconds.
第二个组件持续10000毫秒。
The third component lasts 15000 milliseconds.
第三个组件持续15000毫秒。
In each child component, I used a spinner. This spinner hides when the data arrives at the component.
在每个子组件中,我使用了一个旋转加载图标。当数据到达组件时,这个旋转加载图标会隐藏。
The problem is that when the data arrives at the first component, all the spinners for the other components hide too.
问题是当数据到达第一个组件时,其他组件的旋转加载图标也会隐藏。
Here's the code you provided:
以下是您提供的代码:
英文:
I have a parent component, and three child components
I send data in these three components, and i use setTimeout to control the time for sending the data
The first component lasts 5000 milliseconds
The second component lasts 10000 milliseconds
The third component lasts 15000 milliseconds
In each child component i used a spinner, this spinner hides when the data arrives to the component
The problem is that when the data arrives to the first component, all the spinners for the other components hide too
export class AppComponent {
public dataOne: string;
public dataTwo: string;
public dataThree: string;
constructor() {
setTimeout(() => {
this.dataOne = "one";
}, 5000);
setTimeout(() => {
this.dataTwo = "two";
}, 10000);
setTimeout(() => {
this.dataThree = "three";
}, 15000);
}
}
<one [data]="dataOne"></one>
<two [data]="dataTwo"></two>
<three [data]="dataThree"></three>
export class OneComponent implements OnChanges {
@Input() data: string;
constructor(private spinner: NgxSpinnerService) {
this.spinner.show();
}
ngOnChanges(changes: SimpleChanges) {
if (changes["data"] && changes["data"].currentValue) {
this.spinner.hide();
console.log(changes["data"].currentValue);
}
}
}
答案1
得分: 1
问题出在ngxSpinnerService,因为它是一个单例服务。文档如下所示:
如果您想要多个ngx-spinner实例,只需在ngx-spinner组件上添加name属性。但在这种情况下,您必须在show/hide方法中传递特定的spinner名称。
致敬
英文:
The problem is the ngxSpinnerService because it is a singleton service. The documentation reads as follows:
If you want multiple ngx-spinner instance, just add name attribute with ngx-spinner component. But in this case, you've to pass that particular name of a spinner in show/hide method.
Regards
答案2
得分: 1
Add NgxSpinnerService
in the provider list for each child component as:
providers: [NgxSpinnerService]
英文:
Add NgxSpinnerService
in the provider list for each child component as:
providers: [NgxSpinnerService]
答案3
得分: 0
只需在每个组件实例中添加一个提供者实例,如下所示:
@Component({
selector: "one",
templateUrl: "./one.component.html",
providers: [NgxSpinnerService] // <---- 在这里添加
})
export class OneComponent implements OnChanges {
@Input() data: string;
constructor(private spinner: NgxSpinnerService) {
this.spinner.show();
}
ngOnChanges(changes: SimpleChanges) {
if (changes["data"] && changes["data"].currentValue) {
this.spinner.hide();
console.log(changes["data"].currentValue);
}
}
}
供您参考:https://stackblitz.com/edit/angular-1hqv61
英文:
Just add an instance as provider in each component instance like so:
@Component({
selector: "one",
templateUrl: "./one.component.html",
providers: [NgxSpinnerService] // <---- ADD THIS
})
export class OneComponent implements OnChanges {
@Input() data: string;
constructor(private spinner: NgxSpinnerService) {
this.spinner.show();
}
ngOnChanges(changes: SimpleChanges) {
if (changes["data"] && changes["data"].currentValue) {
this.spinner.hide();
console.log(changes["data"].currentValue);
}
}
}
For your reference: https://stackblitz.com/edit/angular-1hqv61
答案4
得分: 0
以下是您要翻译的部分:
一个非常简单的解决方案是添加一个计数器,在每次更改时更新计数器,当计数器达到3时,表示所有3个子组件已成功从父组件获取到它们的输入:
@Component({
selector: "one",
templateUrl: "./one.component.html",
providers: [NgxSpinnerService] // <---- ADD THIS
})
export class OneComponent implements OnChanges {
@Input() data: string;
dataChangeCounter = 0;
constructor(private spinner: NgxSpinnerService) {
this.spinner.show();
}
ngOnChanges(changes: SimpleChanges) {
if (changes["data"] && changes["data"].currentValue) {
dataChangeCounter += 1;
if (dataChangeCounter === 3) {
this.spinner.hide();
}
}
}
}
这仅在您只有一个输入属性的情况下有效,如果您需要添加更多输入属性,您需要稍微更改代码。
英文:
A very simple solution would be to add counter ,on each change update the counter and when the counter goes to 3 that means all 3 child components have been successfully got there inputs from parent:-
Component({
selector: "one",
templateUrl: "./one.component.html",
providers: [NgxSpinnerService] // <---- ADD THIS
})
export class OneComponent implements OnChanges {
@Input() data: string;
dataChangeCounter =0;
constructor(private spinner: NgxSpinnerService) {
this.spinner.show();
}
ngOnChanges(changes: SimpleChanges) {
if (changes["data"] && changes["data"].currentValue) {
dataChangeCounter += 1;
if(dataChangeCounter ===3){
this.spinner.hide();
}
}
}
}
This is only valid if you have only that one input property,it won't work for multiple input property ,if you need to add more input properties you will have to change the code slightly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论