html - 为什么我的“onclick()”事件在 Angular 中不起作用?
问题描述
这是我的 HTML 代码:
<div class="form-popup playerOne" id="myForm_input" dir="rtl">
<form action="/action_page.php" class="form-container">
<label for="funcs"><b>انتخاب ورودی</b></label>
<mat-select placeholder="انتخاب ورودی" >
<mat-option *ngFor="let input of inputs" [value]="input">
{{ input }}
</mat-option>
</mat-select>
<button type="submit" class="btn " style="margin-top: 270px;" onclick="setInput($event)">اعمال</button>
<button type="button" class="btn cancel" onclick="closeForm()">انصراف</button>
</form>
</div>
这是我的 component.ts 代码:
setInput(event){
console.log('the selected input is:' , event);
}
但它似乎不起作用,因为我在控制台上看不到任何日志消息。
编辑:
我改变了我的代码如下:
HTML:
<div class="form-popup playerOne" id="myForm_input" dir="rtl">
<form action="/action_page.php" class="form-container">
<label for="funcs"><b>انتخاب ورودی</b></label>
<mat-select placeholder="انتخاب ورودی" [(ngModel)] = "selectedValue">
<mat-option *ngFor="let input of inputs" [value]="input">
{{ input }}
</mat-option>
</mat-select>
<button type="submit" class="btn " style="margin-top: 270px;" (click)="setInput(selectedValue)">اعمال</button>
<button type="button" class="btn cancel" onclick="closeForm()">انصراف</button>
</form>
</div>
TS:
selectedValue: any;
setInput(){
console.log('the selected input is:' , this.selectedValue);
}
但我收到此错误消息:
FlowComponent.html:8 ERROR Error: If ngModel is used within a form tag, either the name attribute must be set or the form
control must be defined as 'standalone' in ngModelOptions.
Example 1: <input [(ngModel)]="person.firstName" name="first">
Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">
at Function.missingNameException (forms.js:5421)
at NgModel._checkName (forms.js:5928)
at NgModel._checkForErrors (forms.js:5905)
at NgModel.ngOnChanges (forms.js:5803)
at checkAndUpdateDirectiveInline (core.js:27784)
at checkAndUpdateNodeInline (core.js:38472)
at checkAndUpdateNode (core.js:38411)
at debugCheckAndUpdateNode (core.js:39433)
at debugCheckDirectivesFn (core.js:39376)
at Object.eval [as updateDirectives] (FlowComponent.html:12)
解决方案
您需要使用事件绑定语法才能使其工作。
<button
type="submit"
class="btn"
style="margin-top: 270px;"
(click)="setInput($event)">اعمال</button>
链接到文档
推荐阅读
- perl - yum install perl 失败的依赖项
- javascript - jquery scrolltop 仅适用于 jquery < 3.0.0
- angular - 角度 e2e 测试:如何测试服务注入(使用)另一个服务
- python - 对话流的意图没有更新谷歌上的操作
- reactjs - 如何使用 react-spring 的转换实现暂停和恢复功能?
- raku - 如何在 Perl 6 中模拟当前时间?
- android - 如何在 Xamarin Android 的屏幕上显示全尺寸视图?
- javascript - 我如何摆脱这个不需要的空间
- apache-kafka - 使 kafka 消费者组处于非活动状态
- php - 将数组数据添加到 curl 帖子字段