【问题标题】:Angular 4, sending data from parent to child component issueAngular 4,将数据从父组件发送到子组件问题
【发布时间】:2018-08-12 13:48:26
【问题描述】:

你好,有人可以向我解释一下如何使它正常工作,因为我在使用 viewChild 指令方面不是很有经验...

这就是交易,我正在开发一个有角度的小型 crud 应用程序,并有一个包含我的数据的填充表,在每一行我都有一个删除按钮,它打开一个小的确认窗口,我已经设法获得模态(ngx-bootstrap 3)点击打开,现在我必须弄清楚如何从我的父组件调用函数,我的http请求与我表上的按钮一起工作,但当我尝试修改它以打开模式并制作单击确认按钮的删除请求...

这是一些代码,在我的父 app.component.html 小表中...

     <table style="width:100%" class="table table-striped" 
      id="billing_history_table">
     <thead class="head">
        <tr>
            <th>Name</th>
            <th></th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let e of employeelist>
            <td>{{e.firstName}}</td>
            <td><a (click)="childModal.show()"><span class="glyphicon 
      glyphicon-trash"></span></a></td>
        </tr>
       </tbody>
      </table>

     <app-modal-component #childModal>

     </app-modal-component>

在我的父组件 app.component.ts 中

    @ViewChild('childModal') childModal: ModalComponent;

这是我想在打开的模式中调用的函数,来自父组件。

        DeleteEmployee(id: string) {
        this._usermanagementservice.deleteEmployee(id).subscribe(res => {
            this.LoadAllEmployees();
        })
        }

Id 来自我的数据库,未在表格中显示,我不知道如何将其传递给 modal

在子组件中我有

  @ViewChild('childModal') public childModal: ModalDirective;

   show() {
    this.childModal.show();
   }
   hide() {
    this.childModal.hide();
  }

在子组件.html中

    <div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" 
    role="dialog" [config]="{backdrop: 'static'}" aria-hidden="true">
    <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-body text-center">
            <p>Do you want to confirm?</p>
            <button type="button" class="btn btn-default" 
    (click)="confirm()">Yes</button>
            <button type="button" class="btn btn-primary" 
    (click)="hide()">No</button>
        </div> 
        </div>
     </div>
   </div>

希望您理解我的问题,子 .html 中的确认按钮应该触发 DeleteEmployee 函数并使用我表中的 Id 发出删除请求... 我知道它必须对输出和事件发射器做一些事情,但我不知道,谢谢:(

【问题讨论】:

    标签: angular modal-dialog ngx-bootstrap viewchild


    【解决方案1】:

    您可以在 Angular 中用作 EventEmitter 和 Input() 来完成此任务。

    在您的子组件中声明两个事件发射器,一个用于点击,一个用于不点击。您可以使用 Input 将 Id 传递给子组件。

     @Input() selectedItem:number;
      @Output() yesClicked: EventEmitter<any> = new EventEmitter();
     @Output() noClicked: EventEmitter<any> = new EventEmitter();
    

    点击是时,您可以在子组件中调用方法。在该方法中,您必须发出创建的事件。

    confirm(){
        this.yesClicked.emit();
    }
    
    hide(){
        this.noClicked.emit();
    }
    

    在子组件 html 中,您必须调用 childComponent 中的“确认”方法来处理“是”点击事件,并调用“隐藏”方法来处理“否”点击事件。

      <div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" 
        role="dialog" [config]="{backdrop: 'static'}" aria-hidden="true">
        <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-body text-center">
                <p>Do you want to confirm?</p>
                <button type="button" class="btn btn-default" 
        (click)="confirm()">Yes</button>
                <button type="button" class="btn btn-primary" 
        (click)="hide()">No</button>
            </div> 
            </div>
         </div>
       </div>
    

    在父 app.component.html 中调用 app.component.ts 方法从模板到子组件发出的事件,如下所示。

       <table style="width:100%" class="table table-striped" 
          id="billing_history_table">
         <thead class="head">
            <tr>
                <th>Name</th>
                <th></th>
            </tr>
          </thead>
          <tbody>
            <tr *ngFor="let e of employeelist>
                <td>{{e.firstName}}</td>
                <td><a (click)="clickedItem=e.Id;childModal.show()"><span class="glyphicon 
          glyphicon-trash"></span></a></td>
            </tr>
           </tbody>
          </table>
    
      <app-modal-component #childModal [selectedItem]="clickedItem"(yesClicked)="DeleteEmployee($event)" (noClicked)="hide()" >
    
         </app-modal-component>
    

    在 app component.ts 中为 clickedItem Id 声明一个变量 并声明删除和隐藏模型的方法。

    clickedItem:number;
    

     DeleteEmployee(id: string) {
            this._usermanagementservice.deleteEmployee(id).subscribe(res => {
                this.LoadAllEmployees();
            })
            }
    

    【讨论】:

    • 这不起作用,当我单击确认时,没有任何反应:/ 我不确定我理解“声明删除和隐藏模型的方法”是什么意思?
    • 不得不改变这个论点,谢谢 Ishara (yesClicked)="DeleteEmployee(clickedItem)"
    • 对不起,我在这里做错了。您应该在子组件中按如下方式发出 id,以便从 $event confirm(){ this.yesClicked.emit(selectedItem); 中取回它} 你在这里所做的也是正确的:)
    猜你喜欢
    • 2018-07-07
    • 2021-02-15
    • 2017-09-13
    • 2020-09-28
    • 2019-09-07
    • 2022-08-02
    • 2020-04-04
    相关资源
    最近更新 更多