nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

*ngIf not working in Angular application for hide/show layout after upgrade to NS3

Open NickIliev opened this issue 9 years ago • 40 comments

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

NickIliev avatar Jun 30 '17 12:06 NickIliev

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).

mcrvaz avatar Jun 30 '17 14:06 mcrvaz

Me to, not only with *ngIf but *ngFor too

danielgek avatar Jun 30 '17 16:06 danielgek

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?

NathanWalker avatar Jun 30 '17 19:06 NathanWalker

Same here.

berchik avatar Jul 02 '17 14:07 berchik

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.

u14040426 avatar Jul 03 '17 13:07 u14040426

After I did the upgrade and had to redo my Angular layout logic I've found these issues with ngIf:

  1. ngIf works as expected if value is available at time of rendering
  2. ngIf break any layout (out of order rendering of ngIf elements) if value was delayed (eg: async call)
  3. 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.

memphisvl avatar Jul 03 '17 22:07 memphisvl

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 avatar Jul 04 '17 10:07 EddyVerbruggen

@EddyVerbruggen Your workaround resolved my issue with multiple *ngIf items better than the visibility workaround. Thanks!

pauly815 avatar Jul 04 '17 11:07 pauly815

Hey, guys! Can you share some code snippets where items are rendered out of order?

sis0k0 avatar Jul 05 '17 07:07 sis0k0

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>

screen shot 2017-07-05 at 09 42 57

EddyVerbruggen avatar Jul 05 '17 07:07 EddyVerbruggen

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="&#xf111;"></Label>
                    <Label *ngIf="!item.selected" col="1" row="0" class="fa text-center select-indicator" text="&#xf10c;"></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="&#xf111;"></Label>
                    <Label [visibility]="!item.selected ? 'visible' : 'collapse'" col="1" row="0" class="fa text-center select-indicator" text="&#xf10c;"></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 avatar Oct 26 '17 13:10 cgebe

@cgebe Same here, but I'm experiencing on Android.

jevenson avatar Oct 26 '17 20:10 jevenson

I found out, this issue only happens in conjuction with RadListView due to not using ObservableArray as underlying data structure.

cgebe avatar Oct 27 '17 09:10 cgebe

+1

spmamidi avatar Nov 10 '17 01:11 spmamidi

+1 Still having this issue, both *ngFor and *ngIf don't re-render for iOS correctly when data changes after the initial render

marcusjrc avatar Dec 13 '17 01:12 marcusjrc

Also seeing this issue on IOS for *ngIf

vultix avatar Dec 31 '17 06:12 vultix

+1

souly1 avatar Jan 21 '18 09:01 souly1

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.

giorgiopiatti avatar Jan 29 '18 12:01 giorgiopiatti

Issue should be reopened.

cgebe avatar Jan 29 '18 13:01 cgebe

Heya, could you please share a playground link or some code snippet that demonstrates the broken behavior?

sis0k0 avatar Jan 29 '18 13:01 sis0k0