【问题标题】:Angular 2 bind HTML inputs to component variables like in Vue?Angular 2 将 HTML 输入绑定到 Vue 中的组件变量?
【发布时间】:2019-03-26 05:37:39
【问题描述】:

所以我已经为此苦苦挣扎了很长时间...我一直在研究文档,但是组件和 DOM 之间有很多不同的指令和通信方式,但只有几个很好的例子...

所以我实际上什至不确定我需要什么。假设我的 tables.component.html 文件中有一个用户输入,如下所示:

<label>Name</label>
<input id="customerName" class="form-control" required>

然后是我的 tables.component.ts 文件,如下所示:

import { Component, OnInit } from '@angular/core';
import { isLoggedIn } from '../../assets/js/auth.js';
import { Router } from '@angular/router';


@Component({
  selector: 'app-tables',
  templateUrl: './tables.component.html',
  styleUrls: ['./tables.component.css']
})

export class TablesComponent implements OnInit {

  customers = [];

  id = ""; // keep outside of object to prevent user changes
  customer_form = {
    name: "",
    job: "",
    address: "",
    birthdate: "",
    email: "",
  }

  constructor(private router: Router) { }

    ...
}

为了简单起见:我想将上面的用户输入绑定到我的组件中的 customer_form.name 变量。我正在寻找 Vue 2.0 模型的等价物,这样如果用户更改输入值,组件值也会更改。我不必在表单中推送数据,因为我们的任务是不设置任何后端...

无论如何,我有点困惑。我阅读了文档,但这使情况变得更糟。一页说我应该在表单中添加一个控制器并在 HTML 底部添加一个脚本,另一页说我必须制作一个应该存储在组件中的表单模板......然后在那里有很多不同的指令来绑定东西。我假设你想为此使用 ngModel,但我似乎无法像我找到的示例中那样让它工作。

提前感谢您的帮助..

【问题讨论】:

    标签: javascript html angular vue.js binding


    【解决方案1】:

    我认为您正在寻找的是 [(ngModel)],它用于双向数据绑定。

    <input 
      id="customerName" 
      class="form-control" 
      required 
      [(ngModel)]="customer_form.name"
      name="name">
    

    PS:要使用[(ngModel)],您必须导入FormsModule,然后将其添加到AppModuleimports 数组或您正在使用它的任何模块中。

    ...
    import { FormsModule } from '@angular/forms';
    ...
    @NgModule({
      imports: [ 
        ..., 
        FormsModule, 
        ...
      ],
      ...
    })
    export class AppModule { }
    

    【讨论】:

    • 我已经尝试过了...这总是在 Web 控制台中出现错误提示“无法绑定到 ,因为它不是已知属性”...
    • 您是否导入了FormsModule 并将其添加到您的@NgModule({})imports 数组中
    • 不,我没有......而且你还必须给输入一个名字......上帝知道为什么。非常感谢
    【解决方案2】:
    Use ngModel for two-way data binding
    put [(ngModel)] on the input to pass the data from & to your property like this:
    
    
    //app.module code
    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule }   from '@angular/forms';         //<---- IMPORTED
    
    import { AppComponent }  from './app.component';
    import { TablesComponent } from './tables.component';  //<---- IMPORTED
    
    @NgModule({
      imports: [
        BrowserModule,
        FormsModule                 //<---- IMPORTED IN MODULE
      ],
      declarations: [
        AppComponent,
        TablesComponent    //<---- IMPORTED
      ],
      providers: [],
      bootstrap: [ AppComponent ]
    })
    export class AppModule { }
    //--------------------------tables.component.html
        <input id="customerName" class="form-control" [(ngModel)]="customer_form.name" required>
    

    【讨论】:

    • 谢谢,但正如我已经告诉其他人回答相同的问题,我得到了无法绑定的错误..
    • 它对我来说很好,我试过了......确保你已经在你的 app.module 中导入了组件并将它放在声明中
    • 我做了...但它没有做任何事情。但是,正如 Mikhail 上面建议的那样,导入 formsmodule 有点作用,但是我收到了上面发布的错误。是不是我必须为输入指定一个特定的名称,即使我没有将它发送到服务器?
    • 确保您的 app.module 包含 TablesComponent 和 FormsModule - 我想查看您的 html 代码和 app.module 代码
    【解决方案3】:

    我创建了一个简单的模板驱动表单绑定示例:https://stackblitz.com/edit/angular-m2tkrf

    请注意,FormsModule 是在 app.module.ts

    中导入的

    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule }   from '@angular/forms';         //<---- IMPORTED
    
    import { AppComponent }  from './app.component';
    import { HeroFormComponent } from './hero-form/hero-form.component';
    
    @NgModule({
      imports: [
        BrowserModule,
        FormsModule                 //<---- IMPORTED IN MODULE
      ],
      declarations: [
        AppComponent,
        HeroFormComponent
      ],
      providers: [],
      bootstrap: [ AppComponent ]
    })
    export class AppModule { }

    【讨论】:

    • 所以...导入 FormsModule 做了一些事情!实际上,我在任何文档中都没有看到这一点。但是,我遇到了这个问题:“如果在表单标记中使用 ngModel,则必须设置名称属性或必须在 ngModelOptions 中将表单控件定义为‘独立’。”我现在应该添加名称标签还是为独立添加模型选项?我不确定这是否会影响任何编辑:我是一个愚蠢的白痴。我只需要导入模块并按字面意思给出输入任何名称。非常感谢!
    • 解决问题的最佳方法是为您添加一个名称属性 ,如下所示:&lt;input [(ngModel)]="model.userName" name="userName"&gt;。您用作标签名称的值在表单中应该是唯一的。 “独立”选项很少使用,可能对极少数情况有用。
    猜你喜欢
    • 2017-01-04
    • 2017-05-05
    • 2016-10-01
    • 1970-01-01
    • 2020-01-25
    • 2017-08-18
    • 2018-08-19
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多