0
<div *ngFor="let message of messages">
  <div *ngIf="message.user===user">

  </div>

 <input [(ngModel)]="message"  />
<button (click)="sendMessage()">Send</button>

Here user is the variable that I have assigned in the component. That is a string value 'user1'. If message.user === user1 then I need to show something but its not working.

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
Sreekar Sree
  • 404
  • 5
  • 14
  • 1
    What are the values of `message.user` and `user`? – Jamie Rees Jun 05 '17 at 12:03
  • 1
    Please add your componet.ts code. – Nehal Jun 05 '17 at 12:09
  • Debug your code. Add an output statement `VALUS ARE '{{message.user}}' and 'user'}}` to your template. Anyway, what are you expecting to happen? The `div` you have the `*ngIf` on is empty.. Also, your top `div` is not closed. –  Jun 05 '17 at 12:18

4 Answers4

1

Based on your current code block, you are binding message(object instance) to input with [(ngModel)] in *ngFor, this will lead to error.

also when you type one character in input, it will turn message(object) to string and then lose the original fields(such as user). Maybe this is the reason for your current situation.

here is simple demo may help get out of this.

Pengyy
  • 37,383
  • 15
  • 83
  • 73
0

you need basic code like this...at least.

<div *ngFor="let message of messages">
  <div *ngIf="message.user === user">
      user: {{ message.user }}
  </div>
</div>
<input [(ngModel)]="message"  />
<button (click)="sendMessage()">Send</button>

Your component should have 'messages' prop as well.

dipak
  • 2,011
  • 2
  • 17
  • 24
0

I have provided some demo ..since u failed to add your .ts file just go through this: https://angular.io/docs/ts/latest/api/common/index/NgIf-directive.html

Always Learn
  • 662
  • 6
  • 14
0

Sorry guys that error was just because i missed out closing one div thanks for your responses

Sreekar Sree
  • 404
  • 5
  • 14