javascript - Angular 提交处理程序不将数据记录到控制台

标签 javascript angular

我创建了以下 Angular 分量:

import { Component } from '@angular/core';
import { FormGroup } from '@angular/forms';

@Component({
    selector: 'weather-search',
    template: `
        <section>
            <form (ngSubmit)="onSubmit(f)" let f="ngForm" class="search__form">
                <label for="search_city">Enter City</label>
                <input ngControl="location" type="text" id="search_city"  placeholder="Enter City" required/>
                <button type="submit">Add City</button>
            </form>
            <div class="locationsearch__info">
                <span class="info">City Found</span>:<span>Kansas</span>
            </div>
        </section>
    `,
    styleUrls: ['./weather-search.component.css']
}) 


export class WeatherSearchComponent {
    onSubmit(form: FormGroup) {
        console.log(form);
    }
}

我在我的 app.component.ts 中使用这个组件,如下所示:

@Component({
  selector: 'app-root',
  template: `
  <weather-search></weather-search>
  <weather-list></weather-list>
  `,
  styleUrls: ['app.component.css']
})

现在的问题是,当我点击提交时,我希望提交将被停止,并且表单数据将被 console.log 编辑,这不是现在发生的事情,只是为了给出一个我的代码的简要预览,请注意我如何有一个局部变量 let f="ngForm" 并在提交时将此变量传递给提交处理程序,如下所示 (ngSubmit)="onSubmit( f)”

到目前为止,虽然在提交时我在控制台中看不到任何内容。为什么我的提交处理程序不起作用?

最佳答案

您必须使用 # 符号来定义局部变量,如下所示:

<form (ngSubmit)="onSubmit(f)" #f="ngForm" class="search__form">

关于javascript - Angular 提交处理程序不将数据记录到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46840581/

相关文章:

php - 目录和动态分页 PHP

javascript - Firefox 中的 "c.defaultView.getComputedStyle() is null"问题

javascript - 如何在 AngularJS 中仅搜索某些对象数据

angular - 在 Angular 通用应用程序的查看页面源中看不到标题和元标记

javascript - Angular 8 : Reactive Form Automap and PatchValue Set Member data

node.js - 无法获取/错误

javascript - 推特 Bootstrap 。折叠子元素

javascript - 如何从 indexeddb 中获取所有值

javascript - 图像占位符与 Angular 1 中的不同

excel - 在 Angular 8 应用程序中导出 Excel 文件时如何定义单元格文本对齐方式?