Я пытаюсь сообщить своему родительскому контроллеру, что в его дочерний компонент был добавлен новый элемент. Я знаю, что для этого мне нужно использовать @Output и источники событий, и я их использую, но, похоже, это не работает. Есть ли другой способ сделать это?
Вот мой дочерний компонент ts
import { Component, OnInit, Input, ViewChild, ElementRef, Output, EventEmitter } from '@angular/core';
import { Namespace } from '../../../models/Namespace';
import { Schema } from '../../../models/Schema'
import { ApiNamespaceService } from '../../../Services/api-namespace.service'
import { Router } from '@angular/router';
@Component({
selector: 'add-namespace',
templateUrl: './add-namespace.component.html',
styleUrls: ['./add-namespace.component.css']
})
export class AddNamespaceComponent implements OnInit {
newNamespace = new Namespace();
selectedSchema = new Schema();
schemas: Schema[];
@Input() namespaces: Namespace[];
@ViewChild('alert', { static: true }) alert: ElementRef;
@ViewChild("nameInput", { static: false }) nameInputRef: ElementRef;
success = false;
@Output() public namespaceCreated = new EventEmitter<Namespace[]>();
constructor(private namespaceService: ApiNamespaceService, private router: Router) {
this.schemas = [{ Name: this.selectedSchema.Name, SchemaData: this.selectedSchema.SchemaData, Id: this.selectedSchema.Id }];
}
ngOnInit() {
this.getSchemas();
}
getSchemas(): void {
this.namespaceService.getAllSchemas().subscribe(schemas => this.schemas = schemas);
}
createNamespace(): void {
if (this.nameInputRef.nativeElement.value != '') {
this.newNamespace.SchemaId = this.selectedSchema.Id;
this.newNamespace.Roles = [];
this.namespaceService
.createNamespace(this.newNamespace)
.subscribe(nmsp => this.namespaces.push(nmsp));
this.success = true;
}
this.namespaceCreated.emit(this.namespaces);
this.router.navigateByUrl('main-view/namespaces');
}
createName(): void {
if (this.nameInputRef.nativeElement.value === '') {
this.newNamespace.Name = 'Null';
}
}
closeAlert() {
this.success = false;
}
Здесь я вызываю функцию в родительском HTML
<div *ngIf="shouldOpen" class="col-6">
<add-namespace [namespaces]="namespaces" (namespaceCreated)="showNewNamespace($event)"></add-namespace>
</div>
А вот родительский компонент ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { ApiNamespaceService } from '../../Services/api-namespace.service'
import { Namespace } from '../../models/Namespace'
import { EditNamespaceComponent } from './edit-namespace/edit-namespace.component';
import { Router } from '@angular/router';
@Component({
selector: 'namespaces',
templateUrl: './namespaces.component.html',
styleUrls: ['./namespaces.component.css']
})
export class NamespacesComponent implements OnInit {
namespaces: Namespace[];
selectedNamespace: Namespace;
shouldOpen = false;
query: string;
filteredNamespaces: Namespace[];
constructor(private namespaceService: ApiNamespaceService, private router: Router) { }
ngOnInit() {
this.getNamespaces();
}
getNamespaces(): void {
this.namespaceService.getAllNamespaces().subscribe(namespaces => {
this.namespaces = namespaces;
this.filteredNamespaces = namespaces;
this.search();
});
}
openAddNamespace() {
this.shouldOpen = true;
}
openNamespace(namespace: Namespace) {
this.shouldOpen = false;
this.selectedNamespace = namespace;
this.router.navigateByUrl('main-view/namespaces/' + this.selectedNamespace.Id);
}
search() {
if (!this.query || !this.query.trim()) {
this.filteredNamespaces = this.namespaces;
return;
}
this.filteredNamespaces = this.namespaces.filter(namespace => namespace.Name.toLowerCase()
.includes(this.query.toLowerCase().trim()));
}
showNewNamespace($event) {
this.namespaces = $event;
}