getViewByID not working


#1

Hello i am trying to get a Id of a view but it is always returning undefined

            <StackLayout marginLeft="70" marginRight="70" marginBottom="60" backgroundColor="#444444" borderRadius="30%" opacity="0.7"
                height="100">
                <Label id ="balance" class="labelBalanceValue" text="52.21€"></Label>
                <Label class="labelTextBalance" text="Available Balance"></Label>
            </StackLayout>

I am trying to get to this label to update the text value.

I am just doing this line of code and it is not working…

import { Page } from "ui/page";
import { Component, OnInit } from "@angular/core";
import { registerElement } from 'nativescript-angular';
import { BottomBar, BottomBarItem, TITLE_STATE, SelectedIndexChangedEventData, Notification } from 'nativescript-bottombar';
import { User } from "../../shared/model/user/user";
import { UserService } from "../../shared/model/user/user.service";
import { MovementService } from "../../shared/model/movement/movement.service";
import { Router, NavigationExtras } from "@angular/router";
import * as colorModule from "tns-core-modules/color";
import { ActivatedRoute } from "@angular/router";
import * as platformModule from "tns-core-modules/platform";
import { Config } from "../../shared/config";
import { Budget } from "../../shared/model/budget/budget";
var view = require("ui/core/view");
var applicationSettings = require("application-settings");

registerElement('BottomBar', () => BottomBar);

@Component({
    selector: "homePage",
    providers: [UserService, MovementService],
    templateUrl: "pages/homePage/homePage.html",
    styleUrls: ["pages/homePage/homePage-common.css", "pages/homePage/homePage.css"]
})
export class HomePageComponent implements OnInit {
    public hidden: boolean;
    public titleState: TITLE_STATE;
    public _bar: BottomBar;
    public inactiveColor: string;
    public accentColor: string;
    public user: User;
    public height: number;
    public width: number;
    public budget: Budget;

    constructor(private router: Router, private page: Page, private route: ActivatedRoute, private userService: UserService, private movementService: MovementService) {
        this.route.queryParams.subscribe(params => {
            this.user = JSON.parse(params["user"]);
        })
        console.log("Inside HomePage");
        console.log("UserToken: " + Config.token);
        this.userService.readBalance(applicationSettings.getString("user.token", "default"))
            .subscribe((res) => {
                this.budget.amount = res;
                console.log("Esta variavel tem o seguinte valor : " + this.budget.amount);

            },
            (error) => alert(error)
            );
        var htmlBalance;
        htmlBalance = view.getViewById(this.page,"balance");
        if (typeof htmlBalance == "undefined") {
            alert("Balance undefined!");
        } else {
            htmlBalance = this.budget.amount;
        }

    }

the variable htmlBalance is always returning undefined… when it was supossed to link to the Html variable.

Does anyone see a problem with it?


#2

solved i was calling this getViewByID when i didnt had my user interface initialized.
i had to run that getViewByID inside ngOnInit() function for it to work!