How to check empty object in angular 2 template using *ngIf

前端 未结 7 1179
逝去的感伤
逝去的感伤 2020-12-04 16:26

I want to check if my object is empty dont render my element, and this is my code:


                      
相关标签:
7条回答
  • 2020-12-04 16:45

    This should do what you want:

    <div class="comeBack_up" *ngIf="(previous_info | json) != ({} | json)">
    

    or shorter

    <div class="comeBack_up" *ngIf="(previous_info | json) != '{}'">
    

    Each {} creates a new instance and ==== comparison of different objects instances always results in false. When they are convert to strings === results to true

    Plunker example

    0 讨论(0)
  • 2020-12-04 16:53

    A bit of a lengthier way (if interested in it):

    In your typescript code do this:

    this.objectLength = Object.keys(this.previous_info).length != 0;
    

    And in the template:

    ngIf="objectLength != 0"
    
    0 讨论(0)
  • 2020-12-04 17:03

    This worked for me:

    Check the length property and use ? to avoid undefined errors.

    So your example would be:

    <div class="comeBack_up" *ngIf="previous_info?.length">
    

    UPDATE

    The length property only exists on arrays. Since the question was about objects, use Object.getOwnPropertyNames(obj) to get an array of properties from the object. The example becomes:

    <div class="comeBack_up" *ngIf="previous_info  && Object.getOwnPropertyNames(previous_info).length > 0">
    

    The previous_info && is added to check if the object exists. If it evaluates to true the next statement checks if the object has at least on proporty. It does not check whether the property has a value.

    0 讨论(0)
  • 2020-12-04 17:10

    You could also use something like that:

    <div class="comeBack_up" *ngIf="isEmptyObject(previous_info)"  >
    

    with the isEmptyObject method defined in your component:

    isEmptyObject(obj) {
      return (obj && (Object.keys(obj).length === 0));
    }
    
    0 讨论(0)
  • 2020-12-04 17:10

    From the above answeres, following did not work or less preferable:

    • (previous_info | json) != '{}' works only for {} empty case, not for null or undefined case
    • Object.getOwnPropertyNames(previous_info).length also did not work, as Object is not accessible in the template
    • I would not like to create a dedicated variable this.objectLength = Object.keys(this.previous_info).length !=0;
    • I would not like to create a dedicated function

      isEmptyObject(obj) {
         return (obj && (Object.keys(obj).length === 0));
      }
      

    Solution: keyvalue pipe along with ?. (safe navigation operator); and it seems simple.

    It works well when previous_info = null or previous_info = undefined or previous_info = {} and treats as falsy value.

    <div  *ngIf="(previous_info | keyvalue)?.length">
    

    keyvalue - Transforms Object or Map into an array of key value pairs.

    ?. - The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined

    DEMO: demo with angular 9, though it works for previous versions as well

    0 讨论(0)
  • 2020-12-04 17:10

    Above answers are okay. But I have found a really nice option to use following in the view:

    {{previous_info?.title}}

    probably duplicated question Angular2 - error if don't check if {{object.field}} exists

    0 讨论(0)
提交回复
热议问题