Skip to content

TextField.android property not available in ngAfterViewInit() lifecycle hook #200

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

Closed
ghost opened this issue Apr 28, 2016 · 6 comments
Closed

Comments

@ghost
Copy link

ghost commented Apr 28, 2016

@ViewChild("email") email: ElementRef;

ngAfterViewInit() {
        let email = <TextField>this.email.nativeElement;
        console.log("Got Native element:", email.android);
}

// Output is null
@hdeshev
Copy link
Contributor

hdeshev commented May 5, 2016

I tried it in a component loaded as the main app component, and it worked. I don't think we guarantee that android will be available at ngAfterViewInit though. You could have a case where the element is loaded in a detached part of the visual tree, and it gets attached (and gets an android instance) later. In that case you are better off subscribing to the loaded event.

@hdeshev hdeshev closed this as completed May 5, 2016
@ghost
Copy link
Author

ghost commented May 5, 2016

I am not familiar with loaded event. do you mean ngOnInit()? It does not work there also. Actually my use case is like this. I am loading a route. Once the route is loaded, I am displaying a form to the user. The input fields in the form should have hint colors different than default. ngAfterViewInit is the hook where we know that input fields must have been initialized, so we can access the native elements. But in this hook, although the native elements are accessible, the android or ios property is not accessible. In your view, what is the best place to change the hint color when form is being loaded?

@sebastianteres
Copy link

I'm on the exact same situation, I'm using the setTextFieldColors function from the Tutorial but I want to change the colors as soon as the view gets rendered.
Can you tell us more about the loaded event or point us to the documentation please?

@ghost
Copy link
Author

ghost commented May 20, 2016

I had to do a hack using settimeout:
<TextField (loaded)="onLoaded()"></TextField>
And

onLoaded() {
        console.log("loaded");
        setTimeout(() => {
            if (this.search) {
                let searchField = <TextField>this.search.nativeElement;
                console.log("fetched search", searchField.android);
                let hintColor = new Color("#fed136");
                setHintColor({ view: searchField, color: hintColor });
            }
        });
    }

This is working. But not sure if this is correct way.

@hdeshev
Copy link
Contributor

hdeshev commented May 25, 2016

@sebastianteres setTextFieldColors should be perfectly usable in a ngOnIint callback.

@veenatic the loaded event is a weird one at the moment. It is a part of the NativeScript core view infrastructure and is fired whenever a view instance gets added to the visual tree. The problem with it is that views get added to the tree very early by the renderer, and there are cases when that event won't fire since the renderer will attach the handler after the view has been added to the visual tree.

I'll open a separate issue for supporting the loaded event in a consistent manner since it looks pretty convenient to just add a <TextField #searchBox (loaded)="myHandler(searchBox)"> binding and avoid dealing with ViewChild decorators and ElementRef's.

@ghost
Copy link
Author

ghost commented May 25, 2016

yes, makes sense to use loaded instead of ngOnInit because the nativeElement property itself is not available in ngOnInit.
Thanks for all the help. {N} rocks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants