简体   繁体   English

启用div的更好方法

[英]Better way of enabling a div

I have two divs which I have to hide or display based on the response I get . 我有两个div,我必须根据收到的响应将其隐藏或显示。 For instance say 例如说

<div *ngIf= "field1 != null ">
  <p>field1 </p>
</div>
<div *ngIf= "field2 != null ">
  <p>field2 </p>
</div>

And my response will be 我的回应是

{
  " field1" : "Field1" ,
  " field2" : null ,

}

Based on my response only my field1 will be visible. 根据我的回答,只有我的field1是可见的。 I want to know is there any better way of doing it . 我想知道有没有更好的方法可以做到这一点。 In case in future if the response get changed I have to change in HTML wherever it is referred. 万一将来如果响应发生更改,无论引用到哪里,我都必须更改HTML。 Can I may it generalized ?? 我可以概括一下吗?

The component: 组件:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
  response = {
    "field1": 1, 
    "field2":null, 
    "field3": "foo", 
    "field4": 42,
    "field5": undefined
  };
  responseProperties = [];
  ngOnInit() {
      //In here for demo purpose.
      //Put this code after getting the "response"
      for(let propertyName in this.response) {
           let value:any = this.response[propertyName];
           if(value || value === false)
           this.responseProperties.push({propertyName, value});
      }
  }
}

The associated template: 关联的模板:

<div *ngFor="let p of responseProperties">
  <p>{{p.propertyName}}: {{p.value}}</p>
</div>

The result: 结果:

field1: 1 栏位1:1

field3: foo field3:foo

field4: 42 栏位4:42

It will be better if you write *ngIf="field1" because if it will be undefined ,null, 0 it will be treated as false (0 of number type). 如果您写*ngIf="field1"会更好,因为如果undefined ,null, 0则为undefined ,null, 0将被视为false (数字类型为0)。 Else if it contains anything it will be treated as true . 否则,如果包含任何内容,将被视为true

If you only want to display a field if it exists, then the *ngIf statements will work fine. 如果只想显示一个存在的字段,那么* ngIf语句将正常工作。 Adding bindings to your < p > tags will make sure that the field properties update dynamically with any changes made. 将绑定添加到<p>标记将确保字段属性随所做的任何更改而动态更新。

Like so: 像这样:

<div *ngIf="field1 !== null ">

  <p>{{field1}}</p>

</div>

<div *ngIf="field2 !== null ">

  <p>{{field2}}</p>

</div>

This will maintain that 这将保持

  • if the field exists, the corresponding < div > will be shown 如果该字段存在,将显示相应的<div>
  • the content of the div will be bound to the value of the corresponding field variable, meaning that if the response changes, you won't have to update the html. div的内容将绑定到相应字段变量的值,这意味着如果响应发生更改,则无需更新html。

    Angular continuously updates the *ngIf's and {{}} tags in the html using its change detection algorithm. Angular使用其更改检测算法不断更新html中的* ngIf和{{}}标签。 If you'd like to understand more about how this works, here is an article that explains the way it works: https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html 如果您想进一步了解它的工作原理,请参阅以下文章,说明其工作方式: https : //blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection- explained.html

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

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