gpt4 book ai didi

angular - 如何将子组件传递给父兄弟组件?

转载 作者:太空狗 更新时间:2023-10-29 17:39:11 27 4
gpt4 key购买 nike

当用户鼠标悬停在 list.component.ts 上时,我想显示来自 bucket-modal.component.ts 的弹出窗口。list.component.tsbucket-modal.component.ts之间如何通信?我的代码在这里。

list.component.ts

@Component({    
selector: 'list',
templateUrl: 'list.component.html',
styleUrls: ['list.component.css'],
})

export class ListComponent implements OnInit {
@Input() state: boolean;
@Output() toggle = new EventEmitter();
onHover() {
this.state = true;
this.toggle.emit(this.state);
console.log("state is ----------" + this.state);
}
onHoverOut() {
this.state = false;
this.toggle.emit(this.state);
console.log("state is------ " + this.state);
}
}

list.component.html

<a (mouseover)="onHover()" (mouseleave)="onHoverOut()">random Link list</a>

listdetails.component.ts

@Component({

selector: 'app-list-detail',
templateUrl: 'app-list.component.html',
styleUrls: ['app-list.component.css'],
})


export class ListDetailComponent implements OnInit {


}

listdetails.component.html

<list [elementslist]="listdetails" listingtype="3"></list>
<list [elementslist]="listdetails" listingtype="3"></list>
<list [elementslist]="listdetails" listingtype="3"></list>
<bucket-modal [(showMeaddBucket)]="show2ClickedBucket" [state]="PopUpshow" (toggle)="PopUpshow=$event"></bucket-modal>

bucket-modal.component.ts

@Component({    
selector: 'bucket-modal',
templateUrl: 'bucket-modal.component.html',
styleUrls: ['bucket-modal.component.css'],
})



export class BucketModalComponent implements OnInit {

@Input() state: boolean;
@Output() toggle = new EventEmitter();
onHover() {
this.state = true;
this.toggle.emit(this.state);
console.log("state is " + this.state);
}
onHoverOut() {
this.state = false;
this.toggle.emit(this.state);
console.log("state is " + this.state);
}
}

最佳答案

我认为最简单的方法是在 BucketModalComponent 中创建一个公共(public)方法,它将显示弹出对话框。有点像

export class BucketModalComponent implements OnInit {
showDialog(): void {
// Open the popup dialog
}
}

然后就可以在listdetails.component.html中调用了:

<list ... (toggle)="modal.showDialog()"></list>
<bucket-modal #modal ... ></bucket-modal>

关于angular - 如何将子组件传递给父兄弟组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com