我使用了嵌套在同一个组件中的组件。当我更改父组件中的复选框时附加函数被正确调用并且事件发射器工作正常但是当我更改子复选框时附加函数被触发但事件发射器不可用(据我所知永远不会与 child 绑定(bind))。我想在更改子项中的复选框时发出数据。
如果有人知道答案请帮我解决这个问题。使用嵌套在同一组件内的组件的术语是什么?
谢谢
这是 stackblitz 链接 https://stackblitz.com/edit/angular-material-normal-tree (请查看控制台)
tree.component.ts
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-tree',
templateUrl: './tree.component.html',
styleUrls: ['./tree.component.css'],
})
export class TreeComponent {
@Input() treeData: any;
@Output() toggleEmitter: EventEmitter<any> = new EventEmitter();
constructor() {}
toggleChild(node) {
console.log('from tree start');
console.log(node);
console.log('from tree end');
this.toggleEmitter.emit(node);
// if ('children' in node && node.children.length) {
// node.isExpand = !node.isExpand;
// }
}
test(node, i) {
node.parentIndex = i;
console.log(node);
// this.toggleEmitter.emit(node);
}
CBchangeEvent(node) {
console.log(node);
this.toggleEmitter.emit(node);
}
}
tree.component.html
<ul *ngIf="treeData">
<li *ngFor="let node of treeData;let i = index;">
<button mat-icon-button="" mattreenodetoggle="" class="mat-icon-button" (click)="toggleChild(node)">
<span class="mat-button-wrapper">
<mat-icon class="mat-icon material-icons" role="img" aria-hidden="true">
{{node.isExpand != 0 ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</span>
</button>
<mat-checkbox class="checklist-leaf-node" (change)="CBchangeEvent(node)">{{node.name}}</mat-checkbox>
<app-tree *ngIf="node.isExpand" [treeData]="node.children" (toggleEmitter)="test(node,i)"></app-tree>
</li>
</ul>
tree.component.css
ul {
list-style-type: none !important;
}
tree.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TreeComponent } from './tree.component';
import {
MatIconModule, MatCheckboxModule, MatFormFieldModule, MatButtonModule
} from '@angular/material';
@NgModule({
imports: [
CommonModule,
MatIconModule,
MatCheckboxModule,
MatFormFieldModule,
MatButtonModule
],
declarations: [TreeComponent],
exports: [TreeComponent]
})
export class TreeModule {
static forRoot() {
return {
ngModule: TreeModule
}
}
}
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
myData = [
{'id':1, 'name':'Main 1','isExpand':false,
'children':[
{'id':1, 'name':'Child 1', 'isExpand':false,
'children':[
{'id':2, 'name':'Test2','isExpand':false}
]
}
]
},
{
'id':2, 'name':'Main 2','isExpand':false
}
]
test(node) {
console.log('from app start');
console.log(node);
console.log('from app end');
if ('children' in node && node.children.length) {
node.isExpand = !node.isExpand;
}
}
}
app.component.html
<app-tree [treeData]='myData' (toggleEmitter)="test($event)"></app-tree>
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TreeModule } from './tree/tree.module';
import {
MdToolbarModule,
MdTabsModule,
MdButtonModule,
MdInputModule,
MdDatepickerModule,
MdNativeDateModule,
MdCheckboxModule,
MdRadioModule,
NoConflictStyleCompatibilityMode
} from '@angular/material';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule, BrowserAnimationsModule, MdToolbarModule, MdTabsModule, MdButtonModule, MdInputModule, MdDatepickerModule, MdNativeDateModule, MdCheckboxModule, MdRadioModule, TreeModule, NoConflictStyleCompatibilityMode ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
最佳答案
事件发射器是这样工作的
Parent passes Data to child
和
Child Emits when to let know parent that Data has been - so that parent can call its call back function
对于 child ,知道 parent 已更改日期
Here there is a data binding happening, just like how *ngif works when ever child check data it will always be latest data.
子组件 Input() 变量将处于与绑定(bind)时传递的变量相同的状态。绑定(bind)负责更新子 UI - 在绑定(bind)变量的状态发生变化时
关于javascript - Angular 8 组件嵌套在同一个组件事件发射器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62093782/