2017-08-15 141 views
0

我是TypeScript的新手,嘗試創建一個簡單的應用程序。同時,我想從HTML元素的值通過document.getElementById方法打字稿,它是引發此錯誤:document.getElementById在TypeScript中不起作用

Reference Error document is not defined.

我使用下面這段代碼:

<TextField hint="UserName" name="usernameID" id="usernameID" style="margin-top:5%"/> 

,並試圖讓它在它的TypeScript方法:

export function onLogin() { 

    var username = document.getElementById('usernameID'); 
    console.log('username :: '+username); 
    frame.topmost().navigate("views/choicepage/choicepage"); 
} 

我不知道我在這裏失蹤。

+0

您使用的角度? – saperlipopette

+0

你沒有使用'document.getElementById'。您正在使用'document.getElementsByName'這是一個不同的函數並返回一個NodeList。順便說一句,'TextField'不是一個有效的HTML元素,所以我不確定它會在任何情況下工作。 –

+0

沒有@saperlipopette,它是本機腳本 –

回答

0

nativescript中不存在文檔。你要麼需要使用@ViewChild()角形或this.page.getViewById香草

+0

嗨@ mast3rd3mon當我在this.page.getViewById('IdName')中傳遞Id時, –

+0

@TariqueShamim你對祖先元素有一個'* ngIf'嗎? – NinjaOnSafari

+0

@NinjaOnSafari他沒有使用角 – mast3rd3mon

0

Nativescript不知道文件對象,它不存在與Nativescript

-1

我能解決這個問題做Android或iOS應用其瀏覽器的事通過將'加載的'屬性添加到「Page」,然後創建一個具有加載屬性的相同名稱的方法,該方法將具有頁面的上下文。以下是代碼。

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded"> 
<!-- 
Stack layout for ordering the HTML element at the login page 
--> 
<StackLayout class="p-20" orientation="vertical"> 

    <!--Fields for getting the username and password for input--> 
    <TextField hint="UserName" id="usernameID" style="margin-top:5%"/> 

    <TextField hint="Password" id="passwordID" autocorrect="false" secure="true" style="margin-top:3%"/> 

    <!--Login button here--> 
    <Button text="login" tap="onLogin" id="logInButton" class="btn btn-primary btn-active" style="margin-top:5%"/> 
    </StackLayout> 

這裏那張打字稿

import { EventData } from 'data/observable'; 
import { Page } from 'ui/page'; 
import { TextField } from 'ui/text-field'; 
import { Button } from 'ui/button'; 

let pageLoaded = (args : EventData) =>{ 
    let page = <Page> args.object; 
    let textValue = <TextField>page.getViewById('usernameID'); 
    let logInButton= <Button>page.getViewById('signUpButton'); 

    logInButton.on('tap',(args:EventData)=>{ 
     console.log('textValue :: '+textValue.text); 
    }); 
} 
export {pageLoaded} 

感謝您的幫助傢伙...乾杯:)

相關問題