2016-06-28 97 views
3

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

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

我试图ngAfterViewInit()(加载)从我的.html文件,但他们似乎并不管用。它给我。我的猜测是我得到这个值,因为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%; 
} 

任何建议或见解,将不胜感激。

回答

0

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

2

我不知道如何在Github上的问题被认为是封闭的解决方法,而问题依然存在有​​关。超时解决方案只是等待DOM加载完成,如果从templateURL加载,则可能需要更多时间,所以没有时间添加。