简体   繁体   English

Angular中如何将对象传递给其他组件 9

[英]How to pass objects to other components in Angular 9

i'm working with Angular 9 and Spring boot, i'm making the create part of the cases module, for that i need to get some data to populate select inputs, the microservices are working properly, it gets the data from the DB, but trying to render the select inputs it doesn't recognize the variable.我正在使用 Angular 9 和 Spring 引导,我正在创建案例模块的一部分,为此我需要获取一些数据来填充 select 输入,微服务正常工作,它从数据库获取数据,但试图呈现 select 输入时它无法识别该变量。

This is the oninit of the add-case.ts这是 add-case.ts 的 oninit

ngOnInit(): void {

    this.loading = true;
    this.isLoading.add(
      this.caseService.createCasesFork().subscribe(
        data => {
          this.casesFork = data;
          this.grupos = this.casesFork.groups;
          this.productos = this.casesFork.products;
          this.tipologias = this.casesFork.tipologies;
          this.intenciones = this.casesFork.intentions;
          this.regiones = this.casesFork.regions;

          this.loading = false;
          console.log(this.casesFork.groups);
      
        },
        (error: HttpErrorResponse) => {
          this.alert.errorIzi(error.message);
        }
      ),
      {key: 'loading'}
    )
  } 

it's working so far, it shows the group.到目前为止它正在工作,它显示了该组。

在此处输入图像描述

the problem is the form component where i want to create the select inputs, because this form is a component.问题是我想在其中创建 select 输入的表单组件,因为此表单是一个组件。

This is what i tried.这是我试过的。

<select formControlName="grupos">
    <option [value]="grupo" *ngFor="let grupo of grupos">
      {{ grupo.name }}
    </option>
</select>

but i get this error但我得到这个错误

在此处输入图像描述

this is the form component ts这是表单组件ts

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Action } from '../../../models/enums';

@Component({
  selector: 'app-cases-form',
  templateUrl: './cases-form.component.html',
  styleUrls: ['./cases-form.component.scss']
})
export class CasesFormComponent implements OnInit {
  @Input() action: Action;
  @Output() submitForm = new EventEmitter<boolean>();
  form: FormGroup;
  methods = Action;
  constructor(private fb: FormBuilder) {}

  ngOnInit(): void {
    this.createForm();
  }

  createForm(): void {
    this.form = this.fb.group({
      grupos: ['', Validators.required],
      name: ['', Validators.required],
      field: ['', Validators.required]
    });
  }

  onSubmit(): void {
    if (this.form.valid) {
      this.submitForm.emit(true);
    }
  }
}

How can i read the property in the template or the form ts?我如何读取模板或表单 ts 中的属性? any help or advice is welcome, thanks欢迎任何帮助或建议,谢谢

Firstly, error says the cause.首先,错误说明了原因。 You have not defined grupos variable in your component so it will throw the error.您尚未在组件中定义grupos变量,因此它会抛出错误。

Assuming add-case.ts file is injectable service which you will have to import in CasesFormComponent and you can use that service in front-end like:假设 add-case.ts 文件是可注入服务,您必须将其导入 CasesFormComponent 并且您可以在前端使用该服务,例如:

<select formControlName="grupos">
    <option [value]="grupo" *ngFor="let grupo of addCaseService.grupos">
      {{ grupo.name }}
    </option>
</select>

Or you can use a method in component that fetch grupos information from addCaseService.或者您可以使用组件中的方法从 addCaseService 获取组信息。

Let me know if it helps.让我知道是否有帮助。 Thanks谢谢

You should get your grupos's data on CasesFormComponent's input您应该在 CasesFormComponent 的输入中获取您的组的数据

@Input grupos :Grupos[] = [];

Then on HTML Code然后上HTML码

<select formControlName="groups">
    <option [value]="grupo" *ngFor="let group of grupos">
      {{ grupo.name }}
    </option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM