简体   繁体   English

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

[英]How to open/show my component on a HTML button click - Angular

I'm making simple application, there are 3 components right now, one Parent component, called MAIN COMPONENT, and two child components, one of the child component is displaying selected vehicles and that works fine, but after selection is done I need to click on add button in my app, to open modal (which is another component) where I should choose a customer/client so I could mark that vehicle's are selected customers ownership.我正在制作简单的应用程序,现在有 3 个组件,一个称为 MAIN COMPONENT 的父组件和两个子组件,其中一个子组件正在显示选定的车辆并且工作正常,但是选择完成后我需要单击在我的应用程序中的添加按钮上,打开模式(这是另一个组件),我应该在其中选择一个客户/客户,以便我可以标记该车辆是选定客户的所有权。

This is how it looks in general:这是它的一般外观:

在此处输入图像描述

So basically when add button is clicked (that add button is part of toolbox component) there should be shown modal (another component which holds customers) where I could choose a client.所以基本上当点击添加按钮(添加按钮是工具箱组件的一部分)时,应该显示模态(另一个包含客户的组件),我可以在其中选择一个客户端。

Component that should shown when add is pressed is: customer.component.html按下添加时应显示的组件是: customer.component.html

Now I will post my code:现在我将发布我的代码:

1.) posting component that should hold clients 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() {
  }
}

I general I've no idea how this should be done, maybe I should place something like this on my main component :我一般我不知道应该怎么做,也许我应该在我的主要组件上放置这样的东西:

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

Thanks guys Cheers谢谢大家干杯

So please keep in mind my question at the end is simple, I need to show a modal which represeting my component when button is clicked..所以请记住我最后的问题很简单,我需要显示一个模式,在单击按钮时代表我的组件..

Thanks Cheers谢谢干杯

as you said it's simple, in the main component for example you add this:正如您所说,这很简单,例如在主要组件中添加以下内容:

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);
  }

}

in the html of the MainComponent you add:在您添加的MainComponent的 html 中:

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

now because you are using bootstrap modal, you need to open it programmatically, i assume that you already using JQuery and it's defined somewhere, so in the ChildComponent you do something like this:现在因为您使用的是引导模式,您需要以编程方式打开它,我假设您已经在使用 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);
  }
}

in the html code of ChildComponent you add this:在 ChildComponent 的 html 代码中添加:

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

i didn't test this code, but i'm just giving you example of how you communicate with components, conditionally display them我没有测试这段代码,但我只是给你一个例子,说明你如何与组件通信,有条件地显示它们

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

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