[英]How do you access the for loop of the parent component within a child component which has the inputs?
So I am stuck on this. 所以我被困在这。 I am trying to get the Parent component to talk or integrate with the child component.
我试图让父组件与子组件进行对话或集成。 Here is the parent component which basically has the for loop used to iterate or generate more links if a user wants to add more or presses the button to add more.
这是父组件,基本上,如果用户要添加更多或按下按钮添加更多,则该组件主要具有for循环,用于循环或生成更多链接。
<div class="section url-wrapper">
<div *ngFor="let url of urls; let i = index;" class="link input-wrapper">
<childComponent></childComponent>
<button class="button bad icon-only" (click)="removeURL(i)">
<i class="far fa-times"></i>
</button>
</div>
</div>
The parent component should only be able to register and display the output of the child component. 父组件仅应能够注册和显示子组件的输出。
This is an example of the child component 这是子组件的示例
<div class="section url-wrap">
<input aria-label="URL Title" placeholder="Title" type="text" [value]="urls[i].title" (ngModel)="urls[i].title" name="url.title.{{ i }}"
(input)="updateTitle(i, $event.target.value)">
<input aria-label="URL" placeholder="https://example.com" type="text" [value]="urls[i].url" (ngModel)="urls[i].url" name="url.url.{{ i }}"
(input)="updateUrl(i, $event.target.value)">
</div>
I need help both allowing the parent component to register input from the child component and being able to iterate from the for loop from the parent if it is possible. 我需要帮助,既要允许父组件注册子组件的输入,又要尽可能地从父循环的for循环中获取帮助。
Please let me know if you need more information such as the component files or clarification
如果您需要更多信息,例如组件文件或说明,请告诉我
The stardard way to let components speack each others is with input-output: 让组件相互区分的标准方法是输入输出:
You can pass values from parent to children with @Input for example: 您可以使用@Input将值从父级传递给子级,例如:
Parent code: 父代码:
<childComponent [someInputValue]="hello"></childComponent>
Children code: 子代码:
@Input() someInputValue; //this property will be "hello"
and you can pass values (after being triggered) from children to parent: 您可以将值(在触发后)从子级传递给父级:
Children code: 子代码:
@Output() itemSelectedOutput: EventEmitter<any> = new EventEmitter();
buttonClicked() {
this.itemSelectedOutput.emit("clicked");
}
Parent code: 父代码:
<childComponent [someInputValue]="hello" (itemSelectedOutput)="someParentMethod($event)"></childComponent>
someParentMethod(event: any) {
console.log(event);
}
You can reach the same thing with ISubscription
but I suggest you to use the way above 您可以使用
ISubscription
达成相同的目标,但我建议您使用上述方式
Hope it can help 希望能有所帮助
The below code & example will demonstrate how data flows from parent -> child -> parent by using the @Input()
and @Output()
directives. 下面的代码和示例将通过使用
@Input()
和@Output()
指令演示数据如何从父级->子级->父@Input()
@Output()
。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<div class="section url-wrapper">
<div *ngFor="let url of urls" class="link input-wrapper">
<app-child [url]="url" (updateUrl)="onUrlUpdate($event)"></app-child>
</div>
</div>
`
})
export class ParentComponent implements OnInit {
public urls = [
{url: "https://example.com", title: "Example1"},
{url: "https://example.com", title: "Example2"},
{url: "https://example.com", title: "Example3"},
]
constructor() { }
ngOnInit() {
}
onUrlUpdate($event) {
// completely overkill, but just used to demonstrate a point
var url = this.urls.find(_url => {
// we can see here that the $event.url is actually the same object as the urls[i] that was
// passed to the child. We do not lose the reference when it is passed to the child or back
// up to the parent.
return $event.url === _url
});
if (url) {
url[$event.prop] = $event.newValue;
}
console.log(`Updated URL's "${$event.prop}" property with the value "${$event.newValue}"`);
}
}
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<div class="section url-wrap">
<input aria-label="URL Title" placeholder="Title" type="text" [value]="url.title"
(input)="handleUrlUpdate($event, 'title')"/>
<input aria-label="URL" placeholder="https://example.com" type="text" [value]="url.url"
(input)="handleUrlUpdate($event, 'url')"/>
</div>
`,
})
export class ChildComponent implements OnInit {
@Input() url; // passed in from parent via [url] property on <app-child>
@Output() updateUrl = new EventEmitter();
constructor() { }
ngOnInit() {
// this.url is now available for the life of the child component (assuming it was passed by the parent)
}
handleUrlUpdate($event, propToUpdate) {
// overkill, but used to demonstrate a point
this.updateUrl.emit({url: this.url, prop: propToUpdate, newValue: $event.target.value});
}
}
I wouldn't do it this way in particular. 我不会特别这样做。 If the children have to know about the parents, then your architecture should be adjusted
如果孩子们必须了解父母,那么应该调整您的建筑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.