简体   繁体   English

Angular TestBed:无法绑定到“ngModel”,因为它不是“ng-select”的已知属性

[英]Angular TestBed: Can't bind to 'ngModel' since it isn't a known property of 'ng-select'

I'm trying to use TestBed from to test a Angular component that has a ng-select in its HTML.我正在尝试使用 TestBed from 来测试一个在其 HTML 中具有 ng-select 的 Angular 组件。 I've first got the error saying that Can't bind to 'items' since it isn't a known property of 'ng-select'.我首先收到错误消息说不能绑定到“项目”,因为它不是“ng-select”的已知属性。 so I've imported NgSelectModule and added it to imports in TestBed configurations.所以我已经导入了 NgSelectModule 并将其添加到 TestBed 配置中的导入中。 It now returns Can't bind to 'ngModel' since it isn't a known property of 'ng-select'它现在返回Can't bind to 'ngModel' 因为它不是 'ng-select' 的已知属性

import { getTestBed, TestBed } from '@angular/core/testing';
import { ProductGenericGridComponent } from './product-generic-grid.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ProductGridService } from './product-generic-grid.service';
import { NgSelectModule } from '@ng-select/ng-select';
import { ProductItemComponent } from './../product-item/product-item.component';

describe('Product Generic Grid Component', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [ProductGenericGridComponent],
            imports: [HttpClientTestingModule, NgSelectModule],
            providers: []
        });
    });

    afterEach(() => {
        getTestBed().resetTestingModule();
    });

    it('should return true', () => {
        const fixture = TestBed.createComponent(ProductGenericGridComponent);
        expect(true).toBe(true);
    });
});

Import the forms module into your testbed.将表单模块导入您的测试平台。

    TestBed.configureTestingModule({
        declarations: [ProductGenericGridComponent],
        imports: [HttpClientTestingModule, NgSelectModule, FormsModule],
        providers: []
    });

Step 1: Install ng-select: npm install --save @ng-select/ng-select第 1 步:安装 ng-select: npm install --save @ng-select/ng-select

Step 2: Import the NgSelectModule and angular FormsModule module第 2 步:导入 NgSelectModule 和 angular FormsModule 模块

import { NgSelectModule } from '@ng-select/ng-select';从'@ng-select/ng-select'导入{ NgSelectModule }; import { FormsModule } from '@angular/forms';从“@angular/forms”导入 { FormsModule };

For me too, it happened while unit testing and the following changes helped to resolve the errors对我来说,它也是在单元测试时发生的,以下更改有助于解决错误

...
beforeEach((() => {
    TestBed.configureTestingModule({
        imports:[
            MatDialogModule,
            BrowserAnimationsModule,
            FormsModule,
            AgGridModule.withComponents([]),
            NgSelectModule
        ],
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法绑定到“ ngModel”,因为它不是“ select”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'select' NG8002:无法绑定到“ngModel”,因为它不是“输入”的已知属性。 Angular 10 - NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'. Angular 10 Angular 10 - 无法绑定到“ngModel”,因为它不是“select”的已知属性 - Angular 10 - Can't bind to 'ngModel' since it isn't a known property of 'select' Angular 2+和Observables:无法绑定到“ ngModel”,因为它不是“ select”的已知属性 - Angular 2+ and Observables: Can't bind to 'ngModel' since it isn't a known property of 'select' 无法绑定到“ ngModel”,因为它不是“ textarea”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'textarea' “无法绑定到‘ngModel’,因为它不是‘输入’的已知属性” - “Can't bind to 'ngModel' since it isn't a known property of 'input' ” 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’ 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'input' 错误:无法绑定到“ ngModel”,因为它不是的已知属性 - Error: Can't bind to 'ngModel' since it isn't a known property of 无法绑定到'ngModel',因为它不是'ng-toggle'的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'ng-toggle'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM