【问题标题】:how to display an object on a click event in angular 2/4?如何在角度 2/4 的点击事件上显示对象?
【发布时间】:2023-03-28 18:15:01
【问题描述】:

当我单击一个按钮时,我想使用插值显示用户名到等待列表中。我的按钮的 html 代码在这里。

<button (click) = "onWaitListSubmit(user)">Add to waiting list</button>

<h2 class="page-header">{{user.name}}</h2>

我的 appcomponent.ts 代码在这里。当我点击它时如何获得显示用户名的按钮?

export class AppComponent{

"user": "name"

onWaitListSubmit(user){

}

【问题讨论】:

    标签: javascript node.js angular typescript


    【解决方案1】:
    <h2 *ngIf="show" class="page-header">{{user.name}}</h2>
    
    show:boolean = false;
    
    onWaitListSubmit(user){
      this.show = true;
    }      
    

    你也只是使用

    <h2 class="page-header">{{user?.name}}</h2>
    
    onWaitListSubmit(user){
      this.user = {name: 'foo'};
    }
    

    那么{{user.name}} 只会在this.user 实际有值时显示名称。

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多