如何在 HTML 按钮单击上打开/显示我的组件 - Angular

问题描述 投票:0回答:1

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

这是一般情况下的样子:

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

按下添加时应显示的组件是:

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...

谢谢大家 干杯

所以请记住我最后的问题很简单,我需要显示一个模式,在单击按钮时重新预设我的组件..

谢谢 干杯

javascript jquery angular bootstrap-modal angular-components
1个回答
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>

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

© www.soinside.com 2019 - 2024. All rights reserved.