Show html tag if Angular2

1

I want to show a label if there is data to show:

 <ng-template *ngIf="enableValidation(param.boolean)">
     <a href="{{param.link}}">
        <clr-icon shape="info-standard"></clr-icon> Acceder
       </a>
 </ng-template>
  enableValidation(exist: any) {
        if (exist === null) {
            return true;
        }
    }

The problem is that the tag is not shown at > have or do not have data. Thanks.

I edit, if it is null that it does not do anything, but, I declare myData and I paint the a ... But I'm not going either ..

 <div *ngIf="{{param.boolean}} === null ; else myData" >   </div>
     <ng-template #myData>
        <a href="{{param.link}}">
        <clr-icon shape="info-standard"></clr-icon> Acceder
          </a>
      </ng-template>
    
asked by EduBw 15.11.2018 в 11:49
source

2 answers

2

As I see it, you want to verify if the property boolean of the object param exists or not to modify the template. Just do:

<ng-template *ngIf="!!param.boolean">
    ...
</ng-template>

Let me explain: the expression !variable returns true if that variable is false or undefined (does not exist or has no value). Therefore, with variable !! we would be denying the previous expression, so! Variable will return true if the variable exists, regardless of its value.

In summary, we use !!variable to see if the variable exists (or if it has any value) and !variable to see if it does not exist (or has no value) or if its value is false .

    
answered by 15.11.2018 в 13:21
1

Validate whether the object is true or different depends on your operation. but be careful if you have an object in a variable

I'll show you an example so you can see below:

let a ={
      var_1 : true,
      var_2 : false, 
      var_3 : null,
      var_4 : undefined, 
      var_5 : new Object()
 }


  
  for (x in a){
    if (a[x]) console.info('el valor para ${x} en a es true  ${a[x]}')
    if (!a[x]) console.warn('el valor para ${x} NO TIENE EL VALOR TRUE ${a[x]}')
  
  }
    
answered by 15.11.2018 в 13:10