简体   繁体   English

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

[英]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()

Working Example Here 这里的工作示例

parent.component.ts parent.component.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 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});
  }

}

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.

相关问题 如何从子组件更改父组件的 State? - How Do You Change Parent Component' State From Child Component? 当父组件中发生事件时,如何从子组件更新父组件中的 state - How do you update state in parent component from a child component when an event takes place in the parent component React Native - 如何从父组件调用子组件的 function(不触发无限循环)? - React Native - how do you call the function of a child component from its parent (without triggering an infinite loop)? 在 vue 中,如何检查是否有父组件订阅了子组件发出的事件? - In vue, how do I check if any parent component has subscribed for an event which child is emitting? 从地图循环内的父组件中删除子组件 - removing child component from parent component within map loop 如何访问子级中父级组件的引用 - How do I access refs of a parent component in the child 如何从父组件中访问子组件的子进程? - How to access child component's child from parent component in react? React JS-如何访问从父组件传递的子组件中的道具 - React JS - How to access props in a child component which is passed from a parent component 在 Reactjs 中,如何从父组件操作子组件的子组件? - In Reactjs, how do you manipulate the children of a child component from a parent component? 如何在父组件中使用来自 Vue.js 子组件的数据? - How do I use data from Vue.js child component within parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM