gpt4 book ai didi

javascript - 如何在具有输入的子组件中访问父组件的 for 循环?

转载 作者:行者123 更新时间:2023-11-29 17:39:17 25 4
gpt4 key购买 nike

所以我坚持这个。我试图让父组件与子组件交谈或集成。这是父组件,如果用户想要添加更多或按下按钮添加更多,它基本上具有用于迭代或生成更多链接的 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>

父组件应该只能注册和显示子组件的输出。

这是子组件的例子

<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>

我需要帮助,既允许父组件注册来自子组件的输入,又能够在可能的情况下从父组件的 for 循环进行迭代。

Please let me know if you need more information such as the component files or clarification

最佳答案

下面的代码和示例将演示数据如何通过使用 @Input()@Output() 指令从父级 -> 子级 -> 父级流动。

Working Example Here

父组件.ts

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}"`);
}

}

child.component.ts

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});
}

}

关于javascript - 如何在具有输入的子组件中访问父组件的 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54138082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com