2017-08-15 953 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} 

感谢您的帮助家伙...干杯:)

相关问题