Eldar
Eldar

Reputation: 330

Angular 4, Conditional display with ngIf

help to fix condition to display data. I get model of M through async service, so i use >model?.something< notation to avoid errors when model is still undefined. title is null or 'somestring'. Something wrong with expression. Whatever form i choose it always display one of cases. I have avoided this type expression early and can not translate correctly to typescript.

import { Component } from '@angular/core';
import { M } from './model';

@Component({
  selector: 'app-test',
  template: `
<p *ngIf="model1?.title !== null">
  Display text if title exist('common string')
</p>
<p *ngIf="model2?.title === null">
  Display text if title equal to null
</p>
  `,
  styleUrls: ['./test.component.css']
})

export class TestComponent {
  model1: M;
  model2: M;
  constructor() {
    this.model1 = {
      title: 'username'
    };
    this.model2 = {
      title: null
    };
  }
}

export class M {
    public title: string;

    public constructor(){}
}

Upvotes: 0

Views: 3059

Answers (1)

sanjay kumar
sanjay kumar

Reputation: 858

Please Try:

<h1 *ngIf="model1.title!==null">
    OK
  </h1>

   <h1 *ngIf="model2.title!==null">
    OK
  </h1>

Upvotes: 1

Related Questions