-
Notifications
You must be signed in to change notification settings - Fork 26.2k
ngIf improvements for Rx #13061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This would be useful in many other places, I wouldn't limit it to ngIf. |
I think it would be better to allow assign result of the async pipe to a local var #12237 |
Pipes are not allowed to do that, and it would not fit with our mental model. |
I am not a huge fan of this proposal as it "encourages" logic in the templates. IMHO I think this logic should be resolved in code for better testability/reusability. In these cases I think it's better to use an object with a default value and manual rx subscription over async pipe(s). Also, I think it's better to remove the repeated |
IMHO It's enough
I agree with @thelgevold if some directive requires something existing inside templates it source of common pitfalls. |
Then we should do the same with all other structural directives. |
But what will be if we write:
Ambiguous thing could happen, will we use 'user' or 'userObs'? 'async' correlates with let-<name_of_variable>it's also the source of common pitfalls. |
@olegdunkan |
@DzmitryShylovich ok, aka alias) but it will stay ambiguous. |
The notion of a ngIf which captures the value of the expression result and makes it available, has been around a while. I have an implementation of it linked below, but I'm pretty sure I saw another implementation prior to this (about a year ago). https://github.com/OasisDigital/angular2-firebase/blob/master/ts/ngWhen.ts It is essentially a blend of ngIf and ngFor, written a long time ago based on a (probably incomplete) understanding of how those things work all the way down. Somewhere I got the impression that this idea had been considered and rejected, so I'm thrilled to see the core team looking at it again now. Very useful addition. |
@mhevery a little bit off topic but as we have discussed about it, django has a for else
|
NgIf syntax has been extended to support else clause to display template when the condition is false. In addition the condition value can now be stored in local variable, for later reuse. This is especially useful when used with the `async` pipe. Example: ``` <div *ngIf="userObservable | async; else loading; let user"> Hello {{user.last}}, {{user.first}}! </div> <template #loading>Waiting...</template> ``` Closes angular#13061
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Goals
When writing Rx style code it is often necessary to write code like this
The issue is that:
async
creates a listener and.?
makes coding cumbersome.What is needed is to be able to subscribe to the observable once and then refer to it in subsequent location. Additionally we need a way to display an alternative template while the observable has not yet yielded a value.
Proposal
ngIf
to assign data to local value.else
feature tongIf
so that an alternative template can be shown until the observable yields a value.NOTE: the microsyntax is consistent with existing implementation of the microsyntax. For expanded form see:
The text was updated successfully, but these errors were encountered: