Hey!
Actually, the issue isn't on ngFor but on how loaded works in NativeScript. Loaded emits when the native view is loaded, even if it means it's in the middle of ChangeDetection. This works sometimes in other places because during navigation and bootstrap the views aren't created synchronously as they are when you're using a delayed ngFor.
ngFor does container.createEmbeddedView(template, null) (null being the context) and then manually patches the context, but loaded is happening during createEmbeddedView, so it passes an invalid context to the event.
The new angular integration has just been released (https://blog.nativescript.org/nativescript-angular-12/index.html) and we're wondering on how to best handle this case. Here are the options and pros/cons:
-
make all events async.
pros: no more events mid-cd and others
cons: events are no longer synchronous and can no longer be easily modified. unloaded will never fire as it fires DURING ngOnDestroy (that removes listeners afterwards)
-
add some kind of event prefix that will fire events async (ngSafe-loaded)='loaded(data)'
pros: more power to developers
cons: a bit frustrating to have this issue happen and have to use this as a workaround
-
treat loaded differently and always fire it async
pros: same API and everything
cons: loaded may fire after unloaded! If you create and destroy a component in the same task it may happen. This could get hairy quickly. Other events could also be affected but only loaded is patched
-
Do nothing. Developers can solve this pretty easily by adding a custom directive like safeLoaded that is an @Output with a setTimeout(emit(),0). We can then add this to the documentation.
It seems there isn't an easy way out of this. On one hand, the event is firing correctly when it should, on the other, it's frustrating to use loaded and have it work differently depending on how it's used. I'm open to suggestions on how to best handle this scenario.
Part of me says we should make clear that loaded and unloaded are not "safe" events, and another part says we should make them behave like the web, which might just be impossible for unloaded.