简体   繁体   English

Ionic 2 - 属性在类型错误上不存在

[英]Ionic 2 - Property does not exist on type error

I am developing something in Ionic 2 and am stuck at a simple thing. 我正在开发Ionic 2中的东西,并且陷入了一件简单的事情。

I have a Textbox where I want to type a text in and where the text should appear. 我有一个文本框,我想在其中键入文本和文本应出现的位置。 I used this code from this website and modified it a bit. 我使用了这个网站上的代码并对其进行了一些修改。

It works in a simple HTML/JS program. 它适用于简单的HTML / JS程序。 I think it has something to do with the fact that I use also Typescript in my Ionic Project. 我认为这与我在Ionic Project中使用Typescript的事实有关。

HMTL: HMTL:

<p>This is a  <b id='TextToChange'>Test</b> </p>
<input type='text' id='userInput' value='Enter Text' />
<input type='button' (click)='changeText()' value='Change Text' />

JS/TS: JS / TS:

changeText() {
var userInput = document.getElementById('userInput').value;
document.getElementById('TextToChange').innerHTML = userInput;
}

Then I get an error in Visual Studio (see screenshot below). 然后我在Visual Studio中收到错误(见下面的截图)。 I then changed the code to this: 然后我将代码更改为:

var userInput = document.getElementById('userInput').nodeValue;

In VS I don't get an error anymore but if I press the button the program won't grab the value so it lets the original value vanish and stays blank. 在VS中我不再出现错误但是如果按下按钮,程序将不会获取值,因此它会使原始值消失并保持空白。 No error log for this one. 没有错误日志。

Does anyone have an idea? 有没有人有想法?

在此输入图像描述

I came to a solution. 我找到了解决方案。

HTML: HTML:

<p id='TextToChange'>{{value}}</p>
<input #box (keyup.enter)="changeText(box.value)">
<input type='button' (click)='changeText(box.value)' value='Change Text' />

NOTE: Add the following below your export class! 注意:在导出类下面添加以下内容! JS/TS: JS / TS:

value = '';
changeText(value: string) { this.value = value; }

You can either press Enter or you can press on the Button to display the text. 您可以按Enter键,也可以按下按钮显示文本。

Use: 采用:

var userInput = document.getElementById('userInput').innerText;

Warning: I would strongly recommend you to read the documentation about Angular 2 Data binding . 警告:我强烈建议您阅读有关Angular 2数据绑定的文档。

Under the section "Binding syntax" you can see examples of how this is done using Angular 2. This also applies for Ionic 2. You should really do it the way it is shown there. 在“绑定语法”部分下,您可以看到使用Angular 2完成此操作的示例。这也适用于Ionic 2.您应该按照它在那里显示的方式进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ionic2错误类型“窗口”上不存在属性“ openDatabase” - Ionic2 error Property 'openDatabase' does not exist on type 'Window' 离子2:“属性&#39;号&#39;&#39;在类型&#39;typeof上不存在” - Ionic 2 : “Property 'number' does not exist on type 'typeof” Ionic / Angular HttpClient没有获取类型数据:错误类型“ {}”上不存在属性“名称” - Ionic / Angular HttpClient getting no type data : error Property 'name' does not exist on type '{}' 当属性存在时,类型错误时属性不存在 - Property does not exist on type error when the property does exist Ionic 2 RC0属性&#39;tz&#39;在&#39;typeof moment&#39;类型上不存在 - Ionic 2 RC0 Property 'tz' does not exist on type 'typeof moment' 离子3中“对象”类型中不存在如何修复属性? - how to fix property does not exist on type 'Object' in ionic 3? 离子 AngularFirestoreCollection 属性“id”不存在类型“QueryDocumentSnapshot”<todo> '</todo> - Ionic AngularFirestoreCollection Property 'id' does not exist type 'QueryDocumentSnapshot<Todo>' Ionic&AngularFireDatabase列表结果-类型“ {}”上不存在属性“ map” - Ionic & AngularFireDatabase list result - Property 'map' does not exist on type '{}' 属性“ catch”在类型“ PromiseLike”上不存在 <void> 离子 - Property 'catch' does not exist on type 'PromiseLike<void>' in ionic 带有 --prod 标志的 Ionic 5 构建失败 - 类型上不存在属性“翻译” - Ionic 5 build with --prod flag fails - Property 'translate' does not exist on type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM