加载页面时,NativeScript无法获取或设置UI属性

时间:2016-06-28 18:56:05

标签: angularjs nativescript angular2-nativescript

我正在使用NativeScript Angular构建应用程序。我需要在加载页面时获取或设置UI组件的属性。但是, ngOnInit ngAfterViewInit (已加载)似乎无效。

我创建了一个NativeScript程序来演示这个问题。该程序有两页,每页有两个垂直堆叠的标签。当我按下每页上面的标签时,程序会加载另一页。在控制台中,它打印出标签的宽度(label.getActualSize().width)。当我切换到另一页时,我可以看到标签的宽度没有问题。我得到的值是 270 。但是,我想要的是在加载页面时看到控制台中打印出的宽度。

我在 .html 文件中尝试了 ngAfterViewInit()(已加载),但它们似乎无法正常工作。它给了我 0 。我的猜测是我得到这个值,因为NativeScript在加载所有组件之前给出了宽度值。我需要解决这个问题。

以下是 page_2.component.ts 的代码,其中包含主要测试部分:

import {Component, ViewChild, ElementRef, OnInit} from "@angular/core";
import {Router} from "@angular/router-deprecated";

import {Page} from "ui/page";

import {Label} from "ui/label";

@Component({
    selector: "page2",
    templateUrl: "pages/page_2/page_2.html",
    styleUrls: ["pages/page_2/page_2-common.css", "pages/page_2/page_2.css"]
})

export class Page2 implements OnInit {

    @ViewChild("label_in_2") label_in_2: ElementRef;

    constructor(private _router: Router, private page: Page) {}

    ngOnInit() {

    }

    ngAfterViewInit() {
        let label = <Label>this.label_in_2.nativeElement;

        console.log("width of the label 2 in ngAfterViewInit: " + label.getActualSize().width); // this gives me 0.
    }

    whatisthewidth(testLabel) {
        // let test = testLabel.nativeElement;

        // testLabel.

        console.log("width of the label 2 in whatisthewidth: " + testLabel.getActualSize().width); // this gives me 0.
    }

    to_page1() {
        let label = <Label>this.label_in_2.nativeElement;

        console.log("width of the label 2: " + label.getActualSize().width); // this gives me 270.

        console.log("to page 1");
        this._router.navigate(["Page1"]);
    }

}

为了完整起见,让我附上该计划的其他部分。

main.ts

import {nativeScriptBootstrap} from "nativescript-angular/application";
import {AppComponent} from "./app.component";

nativeScriptBootstrap(AppComponent);

app.component.ts

import {Component} from "@angular/core";

import {HTTP_PROVIDERS} from "@angular/http";
import {RouteConfig} from "@angular/router-deprecated";
import {NS_ROUTER_DIRECTIVES, NS_ROUTER_PROVIDERS} from "nativescript-angular/router";

import {Page1} from "./pages/page_1/page_1.component";
import {Page2} from "./pages/page_2/page_2.component";

@Component({
    selector: "main",
    directives: [NS_ROUTER_DIRECTIVES],
    providers: [NS_ROUTER_PROVIDERS, HTTP_PROVIDERS],
    template: "<page-router-outlet></page-router-outlet>"
})
@RouteConfig([
    { path: "/Page1", component: Page1, name: "Page1" },
    { path: "/Page2", component: Page2, name: "Page2", useAsDefault: true }
])

export class AppComponent { }

app.css

button, label, stack-layout {
    horizontal-align: center;    
}

button {
    font-size: 36;
}

.title {
    font-size: 30;
    margin: 20;
}

.message {
    font-size: 20;
    color: #284848;
    text-align: center;
    margin: 0 20;
} 

.button_label {
    background-color: gray;

    width: 75%;
    height: 25%;
}

page_1.component.ts

import {Component, ViewChild, ElementRef, OnInit} from "@angular/core";
import {Router} from "@angular/router-deprecated";

import {Page} from "ui/page";

import {Label} from "ui/label";

@Component({
    selector: "page1",
    templateUrl: "pages/page_1/page_1.html",
    styleUrls: ["pages/page_1/page_1-common.css", "pages/page_1/page_1.css"]
})

export class Page1 {

    @ViewChild("label_in_1") label_in_1: ElementRef;

    constructor(private _router: Router, private page: Page) {}

    to_page2() {
        let label = <Label>this.label_in_1.nativeElement;

        console.log("width of the label 1: " + label.getActualSize().width);

        console.log("to page 2");
        this._router.navigate(["Page2"]);
    }

}

page_1.html

<StackLayout>
    <Label text="to page 2" class="button_label" (tap)="to_page2()"></Label>
    <Label #label_in_1 text="second label" class="test_label"></Label>
</StackLayout>

PAGE_1-common.css

Label.test_label {
    background-color: blue;

    width: 75%;
    height: 20%;
}

page_2.html

<StackLayout>
    <Label text="to page 1" class="button_label" (tap)="to_page1()"></Label>
    <Label #label_in_2 text="second label" class="test_label" (loaded)="whatisthewidth(label_in_2)"></Label>
</StackLayout>

page_2.common.css

Label.test_label {
    background-color: yellow;

    width: 75%;
    height: 20%;
}

任何建议或见解都将受到赞赏。

2 个答案:

答案 0 :(得分:2)

在问题仍然存在的时候,我不知道Github上的问题是如何通过解决方案被认为是关闭的。超时解决方案只是等到DOM加载,如果从templateURL加载,可能会有更多时间,因此没有时间添加。

答案 1 :(得分:0)

我认为这个问题与this Github问题

有关