【发布时间】:2019-11-08 15:38:56
【问题描述】:
我正在尝试以选择形式显示来自表格英雄和“名词”的数据 这是客户的名字。
在我的第二个表格中,我想在向它们添加产品时展示它们 将客户归于他们。
谁能帮我解决这个问题?我是角度新手:)
heroes.component.ts
import { Component, OnInit } from '@angular/core';
import { HeroesService } from '../../services/heroes.service';
import { HeroeModel } from '../../models/heroe.model';
import {AngularFireDatabase } from 'angularfire2/database';
import Swal from 'sweetalert2';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
heroes: HeroeModel[] = [];
cargando = false;
constructor(private heroesService: HeroesService) {}
ngOnInit() {
this.cargando = true;
this.heroesService.getHeroes().subscribe(resp => {
this.heroes = resp;
this.cargando = false;
});
}
borrarHeroe(heroe: HeroeModel, index: number) {
Swal.fire({
title: 'Uwaga!',
text: `Czy chcesz trwale usunąć kontrahenta ${heroe.nombre}?`,
type: 'warning',
showConfirmButton: true,
showCancelButton: true
}).then(resp => {
if (resp.value) {
this.heroesService.deleteHeroe(heroe.id).subscribe(resp => {
this.heroes.splice(index, 1);
});
}
});
}
}
add-products.component.ts
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import Swal from 'sweetalert2';
import { Observable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { ADDProductsModel } from 'src/app/models/add-products.model';
import { ProductsService } from 'src/app/services/products.service';
import { HeroesComponent } from 'src/app/pages/heroes/heroes.component';
import { HeroeComponent } from 'src/app/pages/heroe/heroe.component';
@Component({
selector: 'app-add-products',
templateUrl: './add-products.component.html',
styleUrls: ['./add-products.component.css']
})
export class ADDProductsComponent implements OnInit {
ADDProducts: ADDProductsModel = new ADDProductsModel();
constructor(
private ProductsService: ProductsService,
private route: ActivatedRoute
) {}
ngOnInit() {
const id = this.route.snapshot.paramMap.get('id');
if (id !== 'new') {
this.ProductsService.getADDProducts(id).subscribe((resp: ADDProductsModel) => {
this.ADDProducts = resp;
this.ADDProducts.id = id;
});
}
}
guardar(form: NgForm) {
if (form.invalid) {
console.log('Nieprawidłowy format');
return;
}
Swal.fire({
title: 'Ładowanie',
text: 'Trwa zapis...',
type: 'info',
allowOutsideClick: false
});
Swal.showLoading();
let peticion: Observable<any>;
if (this.ADDProducts.id) {
peticion = this.ProductsService.actualizarADDProducts(this.ADDProducts);
} else {
peticion = this.ProductsService.crearADDProducts(this.ADDProducts);
}
peticion.subscribe(resp => {
Swal.fire({
title: this.ADDProducts.number,
text: 'Dodano pomyślnie',
type: 'success'
});
});
console.log(form);
console.log(this.ADDProducts);
}
}
【问题讨论】:
-
有什么问题?
-
我不知道如何下载数据以将表单填写到另一个组件:(我已经尝试处理这个问题几个小时了。
-
您可以使用
@input和@output指令进行组件交互。您可以使用@input将数据发送到子组件,并且从子组件到父组件您可以使用@output发送事件发射器。或者,您也可以使用 rxJS Subject .. 在您的情况下,您可以考虑您的两个组件 heros 并使用这种方法添加项目交互。 -
@GaurangDhorda 它可以与 Firebase 一起使用吗?
-
是的,首先您需要从 firebase 获取所有数据,然后将数据传递给组件属性,并且该数据再次用作子组件的输入数据,
<child- cmp [childData]="dataFromFirebase">这是您的子组件。在子组件中获取这样的数据..@input('childData') childPropData;然后你可以使用 html 中的数据,如{{ childPropData.fieldName}}
标签: javascript angular firebase firebase-realtime-database