[英]submit, keyup.enter not working on angular 10
I am learning angular from basic in my code I want to send text from text box on enter
to function when I tried with just keyup
keydown
it was working fine but when try to use submit
keyup.enter
the whole component disappear我正在我的代码中从基本学习角度我想在
enter
时从文本框中发送文本以在我尝试使用keyup
keydown
时它工作正常但是当尝试使用submit
keyup.enter
时整个组件消失
diagnos.component.html:诊断.component.html:
<input type="text" #nameText (keyup.enter)="addItem(nameText.value)">
<ul>
<li *ngFor="let name of names">{{name}}</li>
</ul>
diagnos.component.ts:诊断.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-diagnos',
templateUrl: './diagnos.component.html',
styleUrls: ['./diagnos.component.css']
})
export class DiagnosComponent implements OnInit {
names = ['abc','xyz'];
constructor() { }
ngOnInit(): void {
}
addItem(val){
this.names.push(val);
}
}
when used keyup
only:仅使用
keyup
时:
when used keyup.enter
or submit
after pressing enter
or submitting:当使用
keyup.enter
或按enter
或submit
后提交时:
after enter or submit whole component disappeared even that ul
also and had tried logging on console if that function get call or not but that function doesn't call在输入或提交整个组件后,即使那个
ul
也消失了,并且如果该函数被调用但该函数没有调用,则尝试登录控制台
my project link : https://github.com/ladbhupesh/angularapp我的项目链接: https ://github.com/ladbhupesh/angularapp
I have tried to repro your code and all seems well especially keyup.enter
我试图重现你的代码,一切似乎都很好,尤其是
keyup.enter
Attached herewith is the Stackblitz Demo for your reference.附上Stackblitz Demo供大家参考。
May I know if what's missing?我可以知道缺少什么吗?
Update更新
Based on the provided Github link you mentioned, when looking at the running code, seems like the @Output EventEmitter
affects your (keyup.enter)
which I'm quite not sure what went through your project as I have only scanned fragments of your code but you can resolve that issue by supplying this code below:根据您提到的提供的 Github 链接,在查看正在运行的代码时,
@Output EventEmitter
似乎影响了您的(keyup.enter)
,我不太确定您的项目经历了什么,因为我只扫描了您的代码片段但您可以通过提供以下代码来解决该问题:
AppComponent应用组件
...
changePage(val) {
if (typeof val === 'string') this.pageName = val;
}
What happened was, the event from DiagnosisComponent's keyup.enter
was consoled or communicated on your AppComponent's changePage
which it receives an object not string, resulting for the pageName
to reinitialized it's value to the given object, thus, no components are being rendered and left your page empty as there are no pageName
available for the given object.发生的事情是,来自
DiagnosisComponent's keyup.enter
的事件在您的AppComponent's changePage
上进行了AppComponent's changePage
或通信,它接收一个对象而不是字符串,导致pageName
将其值重新初始化为给定的对象,因此,没有组件正在呈现并离开您的页面为空,因为给定对象没有可用的pageName
。
You can check it out by performing console.log(val)
inside your AppComponent's changePage(val) {...} function
您可以通过在
AppComponent's changePage(val) {...} function
执行console.log(val)
来检查它
This is how you trigger function with the enter key ,这就是你用回车键触发功能的方式,
<input type="text" (keyup.enter)="function()">
not不是
(keyup.enter.p)
(keyup.enter) 在容器 div 中添加而不是在输入中时为我工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.