html
<input #box (keyup.enter)="Character(box.value)"/>
typescript
Character(value: string) {
console.log("print this " + value);
}
我在网上的两个不同地方确认了 typescript 代码,并从此处的另一个答案中获取了 html 代码,即使 typescript 函数执行“值”,也不会打印到控制台。我什至改变了我的浏览器主题,认为这在某一时刻会产生干扰。 (因为它改变了文本框的颜色和一些网站文本与框混合)
更新 正如您从所选答案中可以看出的那样,我没有意识到我的问题是我有两个文本框导致了这个问题,因为值不只是传递给两个文本框,因为我假设。
最佳答案
这按预期工作:
引用:https://stackblitz.com/edit/angular-jcrcdu?file=src%2Fapp%2Fapp.component.html
app.component.html
<input #box (keyup.enter)="Character(box.value)"/>
<p>{{box1name}}</p>
<input #box2 (keyup.enter)="onEnter(box2.value)"/>
<p>{{box2name}}</p>
应用程序组件.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
box1name = 'Angular';
box2name = 'Angular2';
Character(value: string) {
this.box1name="print this for box1 :" + value;
console.log(this.box1name);
}
onEnter(value: string) {
this.box2name="print this for box2: " + value;
console.log(this.onEnter);
}
}
关于javascript - Angular 无法将用户输入从 html 传递给 typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53078801/