简体   繁体   English

如何在2个组件之间共享触发器Angular 2

[英]How to share an trigger between 2 components Angular 2

form.components.ts form.components.ts

import {Component, animate, state, style, trigger, transition} from "@angular/core";

@Component({
    selector:'form',
    templateUrl: 'assets/template/form.html',
    inputs : ['departure','destination'],
    animations:[
        trigger('focusPanel',[
            state('inactiv',style({
                bottom: '-999px',
                display: 'none',
                position: 'fixed',
                background: '#eee',
                width: '100%',
                height: '100%',
            })),
            state('activ',style({
                bottom: '0px',
                display: 'block',
                background: '#eee',
                position: 'fixed',
                'z-index' : '2',
                width: '100%',
                height: '100%',
            })),
            transition('inactiv => activ', animate('600ms ease-in')),
            transition('activ => inactiv', animate('600ms ease-out'))
        ])
    ]
})
export class FormComponents{
    state : string = 'inactiv';
    OpenModal(){
        this.state = 'activ';
    }
    CloseModal(){
        this.state = 'inactiv';
    }
}

form.html form.html

<div class="search-fliht-input" placeholder="Orașul de plecare" id="air-port-from" (click)="OpenModal()"></div>

search.component.ts search.component.ts

import {Component} from "@angular/core";

@Component({
    selector:'search',
    templateUrl: 'assets/template/search.html',
})
export class SearchComponent{
}

search.html search.html

<div class="city-select-container open" [@focusPanel]="state" (click)="CloseModal()"><div>

The problem is that i don't know how to share the trigger between components, the trigger button is in form component and the div that needs to appear is in search component 问题是我不知道如何在组件之间共享触发器,触发器按钮在表单组件中,需要显示的div在搜索组件中

You should use an Output in your form component to send the event in your upper view, and implement a method in this view which would update a flag which is an input of SearchComponent, for example. 您应该在表单组件中使用Output来在上方视图中发送事件,并在该视图中实现一个方法,该方法将更新作为SearchComponent输入的标志。 This input would be used by SearchComponent do display or not the div. 此输入将由SearchComponent使用,而不显示div。

This is my Code 这是我的代码

form.components.ts form.components.ts

import {Component, Output, EventEmitter} from "@angular/core";

@Component({
    selector:'form',
    templateUrl: 'assets/template/form.html',
    inputs : ['departure','destination']
})
export class FormComponents{
    @Output() onOpenModal = new EventEmitter();
    TriggerModalEvent(e : any){
        this.onOpenModal.emit(e);
    }
}

form.html form.html

<div class="search-fliht-input" placeholder="Orașul de plecare" id="air-port-from" (click)="TriggerModalEvent($event)"></div>

search.component.ts search.component.ts

import {Component} from "@angular/core";

@Component({
    selector:'search',
    templateUrl: 'assets/template/search.html',
   animations:[
    trigger('focusPanel',[
        state('inactiv',style({
            bottom: '-999px',
            display: 'none',
            position: 'fixed',
            background: '#eee',
            width: '100%',
            height: '100%',
        })),
        state('activ',style({
            bottom: '0px',
            display: 'block',
            background: '#eee',
            position: 'fixed',
            'z-index' : '2',
            width: '100%',
            height: '100%',
        })),
        transition('inactiv => activ', animate('600ms ease-in')),
        transition('activ => inactiv', animate('600ms ease-out'))
    ])
})
export class SearchComponent{
    state : string = 'inactiv';

    OpenModal(){
       this.state = 'activ';
    }

    CloseModal(){
       this.state = 'inactiv';
    }
}

search.html search.html

<form (onOpenModal)="OpenModal($event)"></form>

<div class="city-select-container open" [@focusPanel]="state" (click)="CloseModal()"><div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM