【问题标题】:How to show heading based on value in angular?如何根据角度值显示航向?
【发布时间】:2018-06-28 08:01:53
【问题描述】:

我正在尝试根据 item._id 更改标题。我存储在组件中的项目。 这是我的 html

<h1 mat-dialog-title>{{item._id ? "Update" : "Add"}} animal</h1>

下面是我的 dialog-overview-example.ts

import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';

/**
 * @title Dialog Overview
 */
@Component({
  selector: 'dialog-overview-example',
  templateUrl: 'dialog-overview-example.html',
  styleUrls: ['dialog-overview-example.css'],
})  
export class DialogOverviewExample {

  animal: string;
  name: string;
  item:string;

  constructor(public dialog: MatDialog) {}

  openDialog(): void {

  item =[{"_id": "2","animal":"lion","weiht":"100"}];
    let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '250px',
      data: { name: this.name, animal: this.animal,item: this.item }
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
      this.animal = result;
    });
  }

}

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {

  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) public data: any) { }

  onNoClick(): void {
    this.dialogRef.close();
  }

}

Demo

如果 _id 存在于项目标题中,则应显示更新动物,否则应显示在我们的案例中添加动物 id 已存在于项目中,因此应显示更新动物..帮帮我

【问题讨论】:

    标签: javascript angularjs angular2-forms angular5


    【解决方案1】:

    我可以看到您已将组件内的项目定义为数组。所以你必须在 HTML 中使用下面的代码

    <h1 mat-dialog-title>{{item[0]._id ? "Update" : "Add"}} animal</h1>
    

    【讨论】:

    • ERROR ReferenceError: item is not defined at DialogOverviewExample.openDialog .getting error above error
    • 可以打印对话框中的数据对象吗?
    猜你喜欢
    • 2016-11-05
    • 2021-09-21
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多