My question duplicat at How to use *ngIf else in Angular?
Asked
Active
Viewed 2,964 times
-2
-
What's the problem? – Günter Zöchbauer Aug 25 '17 at 09:29
-
2use == (or better ===) instead of = for a comparison – Julian Aug 25 '17 at 09:30
-
What have you tried? Show your template code. – Igor Aug 25 '17 at 09:34
-
Sorry I meant *ngIf in HTML page for Angular2 – Wanadjanan Klinnooch Aug 25 '17 at 09:35
-
{{ menuItem.title }} like this – Wanadjanan Klinnooch Aug 25 '17 at 09:37
-
See my updated answer – Carsten Aug 25 '17 at 09:41
-
That statement does not make much sense. What is the result of that `*ngIf`? Are you trying to show something? Ignoring syntax for a moment, what you have there is 2 equality checks with no output. – Igor Aug 25 '17 at 09:41
-
1Duplicate of https://stackoverflow.com/questions/43006550/how-to-use-ngif-else-in-angular-4/43006589#43006589 – Günter Zöchbauer Aug 25 '17 at 09:43
-
Thx you a lots. – Wanadjanan Klinnooch Aug 25 '17 at 09:45
-
@GünterZöchbauer Not a dupe if use multiple if's, – Swoox Aug 25 '17 at 09:57
-
1@Swoox It was initially very unclear what the question was about until he updated it. – Günter Zöchbauer Aug 25 '17 at 09:59
-
@GünterZöchbauer tbh he didn't update it you just misread it – Swoox Aug 25 '17 at 10:00
-
@Swoox that's not true. At first he had not even HTML in his question. – Günter Zöchbauer Aug 25 '17 at 10:35
-
@GünterZöchbauer Ah yeah I see, sorry. But was before your answer :P – Swoox Aug 25 '17 at 10:38
-
@swoop I didn't check properly after I came back from the search. Probably just too many open tabs ... – Günter Zöchbauer Aug 25 '17 at 10:40
2 Answers
3
Wait it's simpler then that. In template:
<div *ngIf = "name == 'a'">if a</div>
<div *ngIf = "name == 'b'">if b</div>
<div *ngIf = "name == 'c'">if c</div>
edit
If you want to make it dynamic do *ngFor let name of names and do ngIf on {{name}}
<div *ngFor="let name of names">
<p *ngIf = "name == {{name}}">Your if</p>
</div> //something like this.

Swoox
- 3,707
- 2
- 21
- 32
-
so What about in case name == d?. I just need use if elseif and else by the way. – Wanadjanan Klinnooch Aug 25 '17 at 09:49
-
-
-
It close to my answer what I want but My question was not clearly. so sorry – Wanadjanan Klinnooch Aug 25 '17 at 10:00
-
@WanadjananKlinnooch No worries some creative mind will fix every problem. – Swoox Aug 25 '17 at 10:01
2
if(name == 'a'){
console.log("1");
} else if(name == 'b') {
console.log("2");
} else {
console.log("other");
}
inline:
*ngIf="name == 'a' ? test('1') : name == 'b' ? test('2) : test('other)"
test is a function that you have to create... or do something else..

Carsten
- 4,005
- 21
- 28