*ngIf not working in Angular application for hide/show layout after upgrade to NS3
From @memphisvl on June 29, 2017 19:56
It used to work on {N} 2.5 and now it seems that the only way to show/hide some elements is using visibility option.
Before
<StackLayout class="page">
<Label *ngIf="isPresent" text="{{showText}}"></Label>
</StackLayout>
Now (Workaround)
<StackLayout class="page">
<Label visibility="{{isPresent ? 'visible' : 'collapse'}}" text="{{showText}}"></Label>
</StackLayout>
Is this expected and from now on I will have to use visibility for UI logic?
Versions
"@angular/animations": "~4.1.0",
"@angular/common": "~4.1.0",
"@angular/compiler": "~4.1.0",
"@angular/core": "~4.1.0",
"@angular/forms": "~4.1.0",
"@angular/http": "~4.1.0",
"@angular/platform-browser": "~4.1.0",
"@angular/platform-browser-dynamic": "~4.1.0",
"@angular/router": "~4.1.0",
"moment": "^2.18.1",
"nativescript-angular": "^3.1.0",
"nativescript-angular-snapshot": "1.5.2-5.5.372.32",
"nativescript-carousel-view": "^2.9.0",
"nativescript-google-maps-sdk": "^2.3.0",
"nativescript-loading-indicator": "^2.3.2",
"nativescript-local-notifications": "^1.2.1",
"nativescript-platform-css": "^1.4.0",
"nativescript-plugin-firebase": "^4.0.2",
"nativescript-pulltorefresh": "^2.0.1",
"nativescript-push-notifications": "^0.1.2",
"nativescript-sidedrawer": "^1.0.6",
"nativescript-theme-core": "~1.0.2",
"reflect-metadata": "~0.1.8",
"rxjs": "~5.3.0",
"tns-core-modules": "^3.1.0",
"zone.js": "~0.8.2"
TNS Paltform 3.1.0
Copied from original issue: NativeScript/NativeScript#4479
I'm having some similiar issues with *ngIf, sometimes the elements are rendered out of order, sometimes aren't even rendered. Seems to be related with the last version (3.1.0).
Me to, not only with *ngIf but *ngFor too
I have found visibility to be more stable generally. Perhaps a directive should be added here out of the box to make visibility more like ngIf in that sense - maybe even call it ngShow which would use the visibility attribute under the hood?
Same here.
I also have a similar issue, where after a http request and setting a boolean to true to show content, the content doesnt show. But what prompted me top try this, was the http request returns a string which is supposed to change a labels text but it didnt change. That is why I wrapped it in a ngIf but then it doesnt show at all.
After I did the upgrade and had to redo my Angular layout logic I've found these issues with ngIf:
- ngIf works as expected if value is available at time of rendering
- ngIf break any layout (out of order rendering of ngIf elements) if value was delayed (eg: async call)
- visibility seem to be good alternative
Overall it was unpleasant to find out that what used to work with {N}2.5 is not working in {N}3.x. At least I know, that am not alone) For anyone wondering about workarounds use [visibility] for Angular html layouts.
A possible workaround for out-of-order rendering could be separating the elements in different <StackLayout>s. My case was this:
<StackLayout>
<GridLayout id="g1" *ngFor>
</GridLayout>
<GridLayout id="g2">
</GridLayout>
</StackLayout>
Because of the ngFor "g2" was rendered before "g1".
This fixed it:
<StackLayout>
<StackLayout>
<GridLayout id="g1" *ngFor>
</GridLayout>
</StackLayout>
<StackLayout>
<GridLayout id="g2">
</GridLayout>
</StackLayout>
</StackLayout>
@EddyVerbruggen Your workaround resolved my issue with multiple *ngIf items better than the visibility workaround. Thanks!
Hey, guys! Can you share some code snippets where items are rendered out of order?
Hi @sis0k0, here's one I hope you find useful:
<StackLayout>
<!-- NOK -->
<StackLayout>
<Label text="label 1"></Label>
<Label text="label 2"></Label>
<Label text="label 3" *ngIf="true"></Label>
<Label text="label 4"></Label>
</StackLayout>
<!-- OK -->
<StackLayout>
<Label text="label a"></Label>
<Label text="label b"></Label>
<StackLayout>
<Label text="label c" *ngIf="true"></Label>
</StackLayout>
<Label text="label d"></Label>
</StackLayout>
</StackLayout>

This issue was initially not about out-of-order rendering. It is about elements, where *ngIf is changed dynamically after the first render of the page, not getting rendered at all. This problem does and still only occur on iOS.
Code examples on Version 3.2.0:
<GridLayout rows="*">
<RadListView row="0" [items]="accounts" pullToRefresh="true" (pullToRefreshInitiated)="refresh($event)" separatorColor="transparent">
<ng-template let-item="item" let-i="index">
<StackLayout>
<GridLayout (tap)="selectAccount(item)"
<Label *ngIf="item.selected" col="0" row="0" class="fa text-center select-indicator" text=""></Label>
<Label *ngIf="!item.selected" col="1" row="0" class="fa text-center select-indicator" text=""></Label>
</GridLayout>
</StackLayout>
</ng-template>
</RadListView>
</GridLayout>
selectAccount(item) toggles the selected boolean attribute. When changed, both directives arent rendered again. When toggling back they arent also not rendered again. Replacing it with [visibility]:
<GridLayout rows="*">
<RadListView row="0" [items]="accounts" pullToRefresh="true" (pullToRefreshInitiated)="refresh($event)" separatorColor="transparent">
<ng-template let-item="item" let-i="index">
<StackLayout>
<GridLayout (tap)="selectAccount(item)"
<Label [visibility]="item.selected ? 'visible' : 'collapse'" col="0" row="0" class="fa text-center select-indicator" text=""></Label>
<Label [visibility]="!item.selected ? 'visible' : 'collapse'" col="1" row="0" class="fa text-center select-indicator" text=""></Label>
</GridLayout>
</StackLayout>
</ng-template>
</RadListView>
</GridLayout>
Leads to not rendering when toggling for the first time, but toggling back renders the elements again!
All in all this issue should not have been closed, because the problem persists, elements with *ngIf are not rerendered when their values are changed dynamically. [visibility] as workaround is not working fully either!
@cgebe Same here, but I'm experiencing on Android.
I found out, this issue only happens in conjuction with RadListView due to not using ObservableArray as underlying data structure.
+1
+1 Still having this issue, both *ngFor and *ngIf don't re-render for iOS correctly when data changes after the initial render
Also seeing this issue on IOS for *ngIf
+1
Any update on this issue, it still persists....
When you have a boolean input as an async variable on the first render it works but if the input changes the UI would not update according to the status.
By the way, I'm using the last version of nativescript and nativescript-angular.
Issue should be reopened.
Heya, could you please share a playground link or some code snippet that demonstrates the broken behavior?