Привет,
простецкая задача - нужно добавить строку в грид. По нажатию на кнопку вызывается диалог, в котором - компонент добавления. Потом пушим наши данные в массив
trustlets и проверяем, запушилось ли - окей, в консоль выводится массив из трех элементов. Кидаем событие в родительский компонент, тянем из него наши
trustlets - там снова два элемента.
Что не так? Спасибо.
AddTrustletComponent (диалог добавления)import { Component, OnInit, Inject, EventEmitter, Output } from '@angular/core';
import { TrustletServiceService } from '../../../service/trustlet/trustlet-service.service';
import { Trustlet } from '../../../model/common.model';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'addTrustlet',
templateUrl: 'add-trustlet.component.html',
providers: [TrustletServiceService]
})
export class AddTrustletComponent implements OnInit {
public name:string;
public repo:string;
private trustlet:Trustlet;
@Output() add = new EventEmitter<Trustlet>();
constructor(private trustletService: TrustletServiceService,
public dialogRef: MatDialogRef<AddTrustletComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
this.trustlet = new Trustlet();
}
onNoClick(): void {
this.dialogRef.close();
}
addTrustlet() {
this.trustlet = {
name: this.name,
repo: this.repo,
}
this.trustletService.addTrustlet(this.trustlet);
this.add.emit(this.trustlet);
}
ngOnInit() {
}
}
TrustletServiceServiceimport { Injectable } from '@angular/core';
import { Trustlet } from '../../model/common.model';
@Injectable()
export class TrustletServiceService {
public trustlets:Array<Trustlet> = [ // mock !!
{ name: 'Trustlet1', repo: 'Repository1' },
{ name: 'Trustlet2', repo: 'Repository2' }
];
constructor() {
}
getTrustlets():Array<Trustlet> {
return this.trustlets;
}
addTrustlet(trustlet:Trustlet) {
this.trustlets.push(trustlet);
console.log(this.getTrustlets()) // три объекта в массиве
}
}
Дашбордimport { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { TrustletServiceService } from '../service/trustlet/trustlet-service.service';
import { CognitoService } from '../service/cognito/cognito.service';
import { AddTrustletComponent } from './dialogs/add-trustlet/add-trustlet.component';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { Trustlet } from '../model/common.model';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css'],
})
export class DashboardComponent implements OnInit {
displayedColumns = ['name', 'repo'];
public dataSource:MatTableDataSource<Trustlet>;
constructor(private cognitoService:CognitoService, private trustletService: TrustletServiceService, public dialog: MatDialog) {
this.dataSource = new MatTableDataSource(trustletService.getTrustlets());
}
openDialog(): void {
let dialogRef = this.dialog.open(AddTrustletComponent);
const sub = dialogRef.componentInstance.add.subscribe(trustlet => {
console.log(this.trustletService.getTrustlets()); // два!! два объекта в массиве, какого черта?
});
dialogRef.afterClosed().subscribe(result => {
console.log(result);
});
}
ngOnInit() {
}
}