简体   繁体   English

Angular 10/Ionic 5 - 将输入数据从模态传递到父组件

[英]Angular 10/Ionic 5 - Passing input data from a modal to a parent component

I am trying to pass data from an ionic modal with an <ion-input></ion-input> .我正在尝试使用<ion-input></ion-input>从离子模式传递数据。 The data returned is then sent to a firebase backend.然后将返回的数据发送到 firebase 后端。

More specifically, I am looking to, on a button press, create a new 'workspace.'更具体地说,我希望在按下按钮时创建一个新的“工作区”。 This workspace has a title, and when a button is clicked, I want to display a modal asking for a title for this new workspace.这个工作区有一个标题,当单击一个按钮时,我想显示一个模式,询问这个新工作区的标题。 On another button press, the title is passed to the parent component, which passes the input data to a new workspace document in firebase.在另一个按钮按下时,标题被传递给父组件,父组件将输入数据传递到 firebase 中的新工作区文档。 It also gives it an entry of the current users uID.它还为其提供了当前用户 uID 的条目。

I am new to angular and ionic, so I am trying 2-way data binding in the modal component, but do not have a firm enough grasp on the ionic/angular mix or the ionic modal to be able to detect the issue.我是 angular 和 ionic 的新手,所以我尝试在模态组件中进行 2 向数据绑定,但对离子/角度混合或离子模态没有足够的把握,无法检测到问题。

Currently, this is the error that is being displayed when the modal button is pressed:目前,这是按下模式按钮时显示的错误:

Uncaught (in promise): Error: This constructor is not compatible with Angular Dependency Injection because its dependency at index 1 of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

There is a workspace interface that I have set to any:有一个工作区界面,我已设置为任何:

export interface Workspace {
  id?: any;
  title?: any;
  description?: any;
  color?: "blue" | "red" | "yellow";
  priority?: any;
}

Here is the parent component .ts with the unnecessary code omitted:这是父组件.ts省略了不必要的代码:

import { Component, OnInit, OnDestroy } from "@angular/core";
import { Workspace } from "../models/workspace.model";
import { Subscription } from "rxjs";
import { WorkspaceService } from "src/app/services/workspace.service";
// ADD THE MODAL
import { ModalController } from "@ionic/angular";
import { WorkspaceModalComponent } from "../modals/workspace-modal.component";

@Component({
  selector: "app-workspaces",
  templateUrl: "./workspaces.component.html",
  styleUrls: ["./workspaces.component.scss"],
})
export class WorkspacesComponent implements OnInit, OnDestroy {
  // HANDLE WORKSPACE SUBSCRIPTION (Some code here not used) 
  workspace: Workspace;
  workspaces: Workspace[];
  sub: Subscription;

  constructor(
    public workspaceService: WorkspaceService,
    public modalController: ModalController
  ) {}

  // GET ALL WORKSPACES AND POPULATE THE WORKSPACES ARRAY
  ngOnInit() {
    this.sub = this.workspaceService
      .getUserWorkspaces()
      .subscribe((workspaces) => {
        this.workspaces = workspaces;
      });
  }

  /**
   * PRESENT THE MODAL FOR CREATING A NEW WORKSPACE
   * RETURN OF AN ASYNC FUNCTION HAS TO BE A PROMISE
   */
  async openWorkspaceModal() {
    const workspaceListModal = await this.modalController.create({
      component: WorkspaceModalComponent,
      // because this is a new workspace, there is no data being passed to the modal component
      componentProps: {},
    });
    workspaceListModal.onDidDismiss().then((data) => {
      if (data) {
        this.workspaceService.createWorkspace({
          title: data,
        });
      }
    });
    return await workspaceListModal.present();
  }
}

Parent html :html

<div class="workspaceGrid" style="padding-right: 30px;">
  <!-- [workspace]="workspace" passes the data down to the child component via an input property *ngFor="let workspace of workspaces" -->
  <app-workspace
    *ngFor="let workspace of workspaces"
    [workspace]="workspace"
  ></app-workspace>
  <ion-button (click)="openWorkspaceModal()">
    Open Modal
  </ion-button>
</div>

Here is the custom workspace service .ts helping create the new workspace in the database, with the user ID and the data gathered from the modal (title prop):这是自定义工作区服务.ts帮助在数据库中创建新工作区,其中包含用户 ID 和从模式(标题属性)收集的数据:

import { Injectable } from "@angular/core";
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore } from "@angular/fire/firestore";
import * as firebase from "firebase/app";
import { switchMap, map } from "rxjs/operators";
import { Workspace } from "../home/models/workspace.model";

@Injectable({
  providedIn: "root",
})
export class WorkspaceService {
  constructor(private afAuth: AngularFireAuth, private db: AngularFirestore) {}

  /**
   *
   * @param data
   * CREATES A WORKSPACE IN THE DATABASE BASED ON THE CURRENTLY LOGGED IN USER
   */
  async createWorkspace(data: Workspace) {
    const user = await this.afAuth.currentUser;
    return this.db.collection("workspaces").add({
      ...data,
      // automatically sets the UID property of the workspace here
      uid: user.uid,
    });
  }
}

The modal component .ts and the modal html :模态组件.ts和模态html

import { Component, Inject } from "@angular/core";
import { ModalController } from "@ionic/angular";
import { WorkspacesComponent } from "../workspaces/workspaces.component";
import { Workspace } from "../models/workspace.model";

@Component({
  selector: "app-workspace-modal",
  templateUrl: "./workspace-modal.component.html",
  styles: [],
})
export class WorkspaceModalComponent {
  constructor(public modalController: ModalController, public data: any) {}

  /**
   * CLOSE THE MODAL ON CLICK
   */
  async closeWorkspaceModal() {
    await this.modalController.dismiss();
  }
}
<ion-header color="primary" mode="ios">
  <ion-toolbar>
    <ion-title>New Workspace</ion-title>
    <ion-buttons slot="end">
      <ion-button (click)="closeWorkspaceModal()">
        <ion-icon slot="icon-only" name="close"></ion-icon>
      </ion-button>
    </ion-buttons>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <ion-item>
    <ion-input
      placeholder="Enter a title for the workspace"
      [(ngModel)]="data.title"
    >
    </ion-input>
  </ion-item>
  <!-- HANDLE SUBMISSION OF THE CONTENT -->
  <ion-button [data]="data.title" (click)="closeWorkspaceModal()"
    >Create Workspace</ion-button
  >
</ion-content>

Thank you for any help you could offer!感谢您提供的任何帮助!

The problem is in the constructor of WorkspaceModalComponent , you're declaring a variable public data: any .问题出在WorkspaceModalComponent的构造函数中,您声明了一个变量public data: any Angular Dependency Injector analyses the type of the variables in the constructor and tries to assign a proper instance according to the type. Angular Dependency Injector 分析构造函数中变量的类型,并尝试根据类型分配适当的实例。 In this case the type is any so Angular can't figure out what dependency to inject.在这种情况下,类型是any ,因此 Angular 无法确定要注入的依赖项。 You should declare this variable as a member of the class instead.您应该将此变量声明为 class 的成员。 Something like this:像这样的东西:

export class WorkspaceModalComponent {
  public data: any;

  constructor(public modalController: ModalController) {}

  /**
   * CLOSE THE MODAL ON CLICK
   */
  async closeWorkspaceModal() {
    await this.modalController.dismiss();
  }
}

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

相关问题 Angular 10/Ionic 5 - 将输入数据从模态传递到父组件,然后提交到 firebase 数据库 - Angular 10/Ionic 5 - Passing input data from a modal to a parent component, then submitting to a firebase database Angular 2将数据从父组件传递到动态模态 - Angular 2 passing data from parent component to dynamic modal 将数据从服务传递到 Angular 中的模态组件 - Passing data from service to modal component in Angular 从父组件Angular 5将数据输入模态 - Inputting data into a modal from a parent component Angular 5 Angular 2将父组件中的数据传递给子组件 - Angular 2 Passing data from Parent component to child Angular MDB将模态组件的数据传递给父组件 - Angular MDB pass the data from modal component to the parent component 数据未从 Angular 中的父组件传递到子组件 - Data not passing to child component from parent component in Angular angular中的父组件向子组件传递数据 - Passing data from parent component to child component in angular Angular2 @Input:将对象数组从父组件传递到子组件 - Angular2 @Input : Passing an objects array from parent to child component 将数据从自定义模式传递到组件中的 html 表 - 角度 - Passing data from custom modal to html table in a component - angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM