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

前端之家收集整理的这篇文章主要介绍了angular – 如何将子组件传递给父级兄弟组件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在list.component.ts上的用户mouSEOver / mouseleave时显示来自bucket-modal.component.ts的弹出窗口.
如何在list.component.ts和bucket-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>

斗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中创建一个公共方法,它将显示弹出对话框.就像是

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

然后你可以在listdetails.component.html中调用它:

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

猜你在找的Angularjs相关文章