当前位置: 首页 > 知识库问答 >
问题:

无法绑定到“ngModel”,因为它不是“input”的已知属性。测验规格ts

凌智
2023-03-14

我已经和另一张票联系在一起了。这个错误只发生在测试中,我已经导入了每个链接消息的FormsMoules。我使用的是Angular 2.2.1。

在ng test中未定义的ngModel我已经导入了FormsMoules和ReactiveFormsMoules,但没有效果。我已经研究了24小时了,但我还没有接近。大多数都与升级有关,我用同样的问题擦拭并重新启动,使用ng生成创建测试模板时,测试模板非常裸露。

我使用angular cli构建我的应用程序(并重建它…)我添加了表单导入,因为它看起来很重要。

这是我的简单模板,请注意,它不是一个表单,而是一个div,没有什么区别“ng serve”看起来合理,因此代码在理论上是正确的。

这是我的表格...

<form class="form-horizontal">
  <div class="form-group">
    <label class="col-sm-2 control-label" for='line1'>Address 1</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" placeholder="Line 1" id='line1' [(ngModel)]='address.line1' required>
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword" class="col-sm-2 control-label" for='line2'>Address 2</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="line2" placeholder="Line 2" [(ngModel)]='address.line2'>
    </div>
  </div>
  <div class="form-inline">
    <div class="form-group">
      <label for="inputPassword" class="col-sm-2 control-label" for='Suburb'>Suburb</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" id="suburb" placeholder="Suburb" [(ngModel)]='address.suburb'>
      </div>
      <div class="col-sm-2">
        <input type="text" class="form-control" id="State" placeholder="State" [(ngModel)]='address.state'>
      </div>
      <div class="col-sm-2">
        <input type="text" class="form-control" id="postcode" placeholder="Postcode" [(ngModel)]='address.postcode'>
      </div>
    </div>
  </div>
</form>

这是我的测试规范,经过大量研究,我添加了CommonModule和ReactiveFormsModule以及FormsBuilder,没有乐趣。

    /* tslint:disable:no-unused-variable */

import { TestBed, async } from '@angular/core/testing';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule, FormBuilder } from '@angular/forms';


import { AddressComponent } from './address.component';
import { Address } from './address';

describe('Component: Address', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [AddressComponent],
      imports: [CommonModule, ReactiveFormsModule],
      providers: [FormBuilder]
    });
  });

  it('should create an instance', () => {
    let component = new AddressComponent();
    expect(component).toBeTruthy();
  });

  it('should handle address', () => {
    expect(true).toBeTruthy();
  });

});

这是我的组件。挺简单的...

import { Input, Component, OnInit } from '@angular/core';
import { Address } from './address';

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

export class AddressComponent implements OnInit {

  @Input() address: Address;

  constructor() { }

  ngOnInit() {
  }

}

我得到的错误是:

"): AddressComponent@4:78
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("iv class="col-sm-10">
      <input type="text" class="form-control" id="line2" placeholder="Line 2" [ERROR ->][(ngModel)]='address.line2'>

共有1个答案

端木涵润
2023-03-14

同时添加表单模块

imports: [CommonModule, ReactiveFormsModule, FormsModule],
 类似资料:
  • 问题内容: 即使未显示组件,启动我的Angular应用程序时也会出现以下错误。 我必须将注释掉,这样我的应用才能正常工作。 我正在查看Hero插件,但与我的代码没有任何区别。 这是组件文件: 问题答案: 是的,就是这样,在app.module.ts中,我刚刚添加了:

  • TestFormComponent应该更新模型值失败错误:模板分析错误:无法绑定到'(NgModel',因为它不是'input'的已知属性。(“][(NgModel]=”ModelValue“/>”) 我已经进口了FormsModule。如果我错过了什么,请指导我

  • 我试图在离子3上使用文本掩膜,但它不起作用,我收到错误消息(无法绑定到文本掩膜,因为它不是离子输入的已知属性)。我在遵循乔什莫罗尼的教程https://www.joshmorony.com/improve-mobile-form-ux-with-input-masks/ 单击此处查看错误消息

  • 我已经在我的应用程序中导入了FormsModule和ReactiveFormsModule。模块,html应该是正确的, 错误 进口 我在运行npm-run-test:local时遇到这个错误,但如果我执行npm-start,它运行得很好。我使用了multiple[(ngModel)],它们都有相同的问题,我只是以这个为例。 我怎样才能解决这个问题?

  • 几天前,我开始使用角项目的离子。我有一个问题,当我服务我的项目.这是错误: NG0303:不能绑定到'ngModel',因为它不是一个已知的属性'离子范围'. 我想用离子范围的亮度普林。这是我的密码。 应用程序。单元ts 我有一些随机组件的共享文件夹和一个随机器。模块来管理它们。 在randomer.module.ts 还有我的brigthness.component.html 亮度组成部分ts:

  • 我试图在Angular 2中实现动态表单。我在动态表单中添加了删除和取消等附加功能。我遵循了以下文档:https://angular.io/docs/ts/latest/cookbook/dynamic-form.html 我对代码做了一些修改。我在这里搞错了。 我如何使这个错误消失? 您可以在这里找到完整的代码:http://plnkr.co/edit/sl949g1hqqrnrur1xxqt?