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
Angular 2.0 Lifecycle hook #5177
Comments
When working with nested properties of objects that may not exist yet, you can do |
Thanks Rob. No it's not working not for nested object nor for the parent one. It seems angular 2.0 expects everything should be exist before rendering. |
can you replicate this in a plunker please? helps to troubleshoot. |
Well not familiar with plunker. So I published the sample in c9.io. |
c9.io seems to require a sign-in which makes it no-go for reproducing issues. Even if you are not familiar with plunker, you can open one from https://angular.io/docs/ (ex. https://angular.io/docs/ts/latest/api/core/NgModel-directive.html). Plunker is the same kind of tool like jsfiddle, codepen, jsbin etc. so if you are familiar with one of those tools you can use them as well. |
Thanks Pawel for the Plunker example. I forked the example and changed it to demonstrate my problem. |
@behzadeshan thnx for the plunk, it makes things more clear. 2 remarks to start with:
|
FYI, in the code there is a propery named meber, You may suggest I should initiate it. But I can't, because I have this problem in my CrudBaseController, it's a base class that other crud controllers inherit it. So it even don't know what property it has to initiate it. In the other hand why I should initiate my properties in Update mode when there value will be passed again and it's means angular should render view twice. Once for initiating values and then for the real values. I think There is should be a flag like |
I am using the newest version alpha.45 in my production code, and has the same problem. |
No, this is not what we are suggesting. The suggestion is to be explicit about what might be null and this is what the elvis operator is for: |
Hmm, actually it won't work with
|
@behzadeshan loadData(){
this.member = {
name: "Behzad Eshan"
};
}
render() {
this.loadData();
this.ready = true;
}
eraseData() {
this.ready = false;
this.member = null;
} |
@pkozlowski-opensource Even if it works, why I should change a lot of assignments in the markup when I can use a single flag or a single method in my code? Imagine in an enterprise project, most of developers will forget to use |
@pkozlowski-opensource Elvis operator is working for |
@behzadeshan I hear what you say about the elvis operator. Personally I'm still on a fence when it comes to its usage vs. what we know from ng1. I'm going to leave this issue open so other can chime-in to express their opinion on Elvis vs. forgiving evaluation of expressions known from ng1. |
@pkozlowski-opensource Thanks . I am not ok with ng1 forgiving evaluation either because of silent failures that it hides. I think there should be a third way to explicitly tell the angular that some properties are nullable, but not in the markup but in code (for example by annotation), because a property may be used in more than one place in markup, and if we want to mention it's nullablity by elvis operator in markup we should change more than one place. In the other hand mentioning a property nullablity in code will save view changes from code changes when setting or removing nullablity of the property. |
@pkozlowski-opensource By the way will you support elvis oprator on ngModel as well? |
My first reaction to |
@e-oz It's not only performance. It's also about being more explicit. It was very hard to debug issues with Angular 1 when an expression did not result in the expected output as the parser was so forgiving. In Angular 2 you'll get a detailed error why an expression can't be evaluated and you can explicitly tell Angular where to be forgiving. |
So will NgModel support the elvis operator in the final release? Right now to address this type of issue that NgModel can't deal with the elvis operator I end up having to create a dummy object and copy all the field inside the object once they become available. It's not really elegant and was wondering if there's a better way to handle this? |
We have the same problem as OP, it happens often in the case of a pre-filled form. Either we have to not use ngModel since the elvis operator doesn't work for now or use a dummy model. We chose to go with dummy model it ends up looking something like this
This is not so elegant but only way found to use the ngModel. Otherwise you end up using {{model?.field}} which works to display the view but then have to take care of updating the model manually either via ngFormModel and control group or just simple document.getElementById none of which seems any better. I agree with OP that an extra lifecycle hook like OnBeforeBinding/OnBeforeRendering would seem to be an easy way to get out of this. If someone else found a way to address this would be interested to know as well. |
As a follow up, after some refactoring the most elegant solution we came up with was to have some kind of json encapsulation service with an empty object that get filled in later with the http call. Somewhere along the betas this started to work. |
Is this about supporting the elvis operator in ng-model? If so maybe it should be renamed? |
Please open a new issue with the most current pb |
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. |
Hi. I have a component for CRUD operations, in Update mode, the data is fetched using ajax after component is loaded, because of it I got this error when view is rendering:
TypeError: Cannot read property '...' of undefined in [null]
It's because my data is not initialized before view rendering/binding so it's null.
I used
ng-if="ready"
in the top of my component's markup and theready
flag is setting after fetching the data. It prevents view binding/rendering before data be ready, This solved the problem here. But I have a same problem in a nested component, so I'm wondered if is there a better way to solve this problem? I took a look at Lifecycle hooks, but there is no hook like OnBeforeBinding/OnBeforeRendering to help me with this issue.Any suggestion?
The text was updated successfully, but these errors were encountered: