如何在HTML按钮上打开/显示我​​的组件,请点击-角度

时间:2018-07-11 21:36:03

标签: javascript jquery angular bootstrap-modal angular-components

我正在制作简单的应用程序,现在有3个组件,一个父组件(称为MAIN COMPONENT)和两个子组件,其中一个子组件正在显示选定的车辆,并且工作正常,但是选择完成后,我需要在我的应用程序中单击添加按钮,以打开模式(这是另一个组件),我应该在该模式中选择一个客户/客户,以便可以标记出该车辆已被选择为客户所有权。

总体上这样子:

enter image description here

因此,基本上,当单击添加按钮(该添加按钮是工具箱组件的一部分)时,应该显示模式(可以容纳客户的另一个组件),供我选择客户。

按下添加时应显示的组件是:customer.component.html

现在我将发布我的代码:

1。)发布应包含客户customer.component.html

的组件
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Clients</h4>
  </div>
  <div class="modal-body item-edit-container">
    <h4 class="modal-title" style="text-align: center;">Find client by ID</h4>
    <div class="form-group">
      <input type="text" class="form-control dash-form-control" id="" placeholder="" autofocus>
      <div style="margin-top: 10px;">
        <select class="form-control dash-form-control select2" style="width: 100%;">
          <option selected disabled>Search for client..</option>
          <option>Person 1</option>
          <option>Person 2</option>
          <option>Person 3</option>
        </select>
      </div>
    </div>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn save-keyboard-btn" data-dismiss="modal"></button>
  </div>
</div>



@Component({
  selector: 'app-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.css']
})
export class ClientComponent implements OnInit {
   ngOnInit() {
  }
}

一般来说,我不知道该怎么做,也许我应该在我的主要组件上放这样的东西:

<app-customer></app-customer> and somehow show it when I click on button add, but I really don't know how to achieve this? Could anyone write me some simple steps which I might follow or whatever...

谢谢大家 干杯

因此请记住,我的问题最后很简单,我需要显示一个模式,该模式可以在单击按钮时重新设置我的组件。

谢谢 干杯

1 个答案:

答案 0 :(得分:0)

正如您所说的那样简单,例如,在主要组件中添加以下内容:

import {Component, OnInit} from'@angular/core';
export class MainComponent implements OnInit{
  public shoud_open = false;

  openChildComponent(){
   this.should_open = true;
  }

  itemSelected(item){
    console.log(item);
  }

}

在您添加的MainComponent的html中:

<button (click)="openChildComponent()">Open</button>
<div *ngIf="shouldOpen">
   <child-component (onItemSelect)="itemSelected($event)" [items]="persons"></child-component>
</div>

现在,因为您使用的是引导程序模态,所以需要以编程方式打开它,我假设您已经在使用JQuery并且它已在某处定义,因此在ChildComponent中您可以执行以下操作:

import {Component, AfterViewInit, OutPut, Input, EventEmitter} from'@angular/core';
export class ChildComponent implements AfterViewInit{
  @OutPut()
  public onItemSelect: EventEmitter<any>;

  @Input()
  public items: Array<any>; 
  constructor(){
    this.onItemSelect= new EventEmitter<any>();
  }
  ngAfterViewInit(){
    $('#modal_id').modal('show');
  }
  selectItem(item){
   this.onItemSelect.emit(item);
  }
}

在ChildComponent的html代码中添加以下代码:

<select multiple name="items" (change)="selectItem($event)">
  <option *ngFor="let item of items" [value]="item">Item</option>
</select>

我没有测试这段代码,但是我只是给您一个示例,说明如何与组件进行通信,有条件地显示它们