简体   繁体   English

ControlContainer 没有提供程序 - Angular 5

[英]No provider for ControlContainer - Angular 5

I am converting a purchased, third-party template into an Angular 5 app, and just ran into an error.我正在将购买的第三方模板转换为 Angular 5 应用程序,但遇到了错误。 I am very new to Angular 5 (I know AngularJS well however) and don't understand what it's trying to tell me?我对 Angular 5 很陌生(但是我很了解 AngularJS)并且不明白它想告诉我什么? It seems to be related to a button which shows/hides the top navbar.它似乎与显示/隐藏顶部导航栏的按钮有关。

Error Message (from browser):错误消息(来自浏览器):

Error: Template parse errors:
No provider for ControlContainer ("imalize-styl-2 btn btn-primary " (click)="toggleNavigation()"><i class="fa fa-bars"></i> </a>
      [ERROR ->]<form role="search" class="navbar-form-custom" method="post" action="#">
        <div class="form-gro"): ng:///AppModule/TopNavigationNavbarComponent.html@4:6

component.html:组件.html:

<div class="row border-bottom">
  <nav class="navbar navbar-static-top" role="navigation" style="margin-bottom: 0">
    <div class="navbar-header">
      <a class="minimalize-styl-2 btn btn-primary " (click)="toggleNavigation()"><i class="fa fa-bars"></i> </a>
      <form role="search" class="navbar-form-custom" method="post" action="#">
        <div class="form-group">
          <input type="text" placeholder="Search for something..." class="form-control" name="top-search" id="top-search">
        </div>
      </form>
    </div>
    <ul class="nav navbar-top-links navbar-right">
      <li>
        <a href="#">
          <i class="fa fa-sign-out"></i> Log out
        </a>
      </li>
    </ul>
  </nav>
</div>

component.ts组件.ts

import { Component, OnInit } from '@angular/core';
import { smoothlyMenu } from '../../../app.helpers';

declare var jQuery: any;

@Component({
  selector: 'app-top-navigation-navbar',
  templateUrl: './top-navigation-navbar.component.html',
  styleUrls: ['./top-navigation-navbar.component.less']
})
export class TopNavigationNavbarComponent implements OnInit {

  toggleNavigation(): void {
    jQuery('body').toggleClass('mini-navbar');
    smoothlyMenu();
  }

  constructor() { }

  ngOnInit() {
  }

}

app.module.ts (this seems to be something a mentioned a lot when I google this, however it is not the Form throwing the error.) app.module.ts (这似乎是我在谷歌上经常提到的东西,但它不是抛出错误的表单。)

...
import { ReactiveFormsModule, FormControl, FormsModule } from '@angular/forms';
...

除了ReactiveFormsModule FormsModule ,还导入FormsModule

Import FormsModule and ReactiveFormsModule in views.module.ts (custome module file) file works for me :views.module.ts (自定义模块文件)文件中导入FormsModule和 ReactiveFormsModule 对我views.module.ts

views.module.ts views.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';


@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    FormsModule,
    ReactiveFormsModule,
    ...
  ],
  declarations: [
    ...
  ],
  exports: [
   ...
  ],
  schemas: [NO_ERRORS_SCHEMA]
})
export class ViewsModule { }

I'm not sure why the error seems to be pointing to the anchor tag outside of the form element, but it was the form element that was causing the error.我不确定为什么错误似乎指向表单元素外部的锚标记,但它是导致错误的表单元素。 Adding FormGroup to the form fixed the problem.将 FormGroup 添加到表单解决了这个问题。

<form role="search" class="navbar-form-custom" [formGroup]="form">

Edit the file "app.module.ts";编辑文件“app.module.ts”; change the following instruction:更改以下指令:

import { ReactiveFormsModule } from '@angular/forms';

with the following:具有以下内容:

import { ReactiveFormsModule, FormsModule } from '@angular/forms';

Change the following piece of code:更改以下代码段:

  imports: [
    BrowserModule,
    ReactiveFormsModule
  ],

with the following:具有以下内容:

  imports: [
    BrowserModule,
    ReactiveFormsModule,
    FormsModule
  ],

If you are trying to display plain HTML form from an Angular component, use ngNoForm in <form> tag like this.如果您尝试从 Angular 组件显示纯 HTML 表单, ngNoForm<form>标签中使用ngNoForm ,如下所示。

<form ngNoForm>
...
</form>

This should prevent Angular from throwing Control Container Error.这应该可以防止 Angular 抛出控制容器错误。

After spending 2 hours on it, i realised that when we import FormModule in app.module as well as in child.module then this kind of error occur.花了 2 个小时之后,我意识到当我们在 app.module 和 child.module 中导入 FormModule 时,会发生这种错误。 Simpley remove FormModule from child.module then error will be resolved :) Simpley 从 child.module 中删除 FormModule 然后错误将被解决:)

Add this line to your component.spec.ts:将此行添加到您的 component.spec.ts:

import {FormsModule} from '@angular/forms';

imports: [
  RouterTestingModule,
  FormsModule
],

I had this issue in Angular(Jasmin) Test cases.我在 Angular(Jasmin) 测试用例中遇到了这个问题。

I have accidentally added angular form modules in the import of my Unit Test at beforeEach, one is FormModule (by angular) and other one is Custom FormModule.我在 beforeEach 的单元测试的导入中不小心添加了 angular 表单模块,一个是 FormModule (by angular),另一个是 Custom FormModule。

I need only Custom FormModule, once I removed the FormModule (by angular) the issue resolved我只需要自定义 FormModule,一旦我删除了 FormModule(按角度),问题就解决了

beforeEach(async(() => {
TestBed.configureTestingModule({
  imports: [CustomTranslateModule.forRoot(),
    CustomRightPanelModule,
    CustomListModule,
    CustomInputTextModule,
    CustomMasterPageModule,
    CustomFormModule,
    FormModule, // this is unnecessary 
    .....................
    .....................

暂无
暂无

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

相关问题 Angular 5:“没有 ControlContainer 的提供者” - Angular 5: “No provider for ControlContainer” 没有ControlContainer的提供者和ControlContainer的提供者 - No provider for ControlContainer and No provider for ControlContainer Angular V11:NullInjectorError:没有 ControlContainer 的提供程序 - Angular V11: NullInjectorError: No provider for ControlContainer Angular2构建简单表单时没有ControlContainer的提供者 - Angular2 No provider for ControlContainer when building a simple form 角度2:使用[class.someClass]会导致`ControlContainer没有提供者!? - Angular 2 : using [class.someClass] causes `No provider for ControlContainer!`? Angular 9 “没有 ControlContainer 的提供者” VSCode 错误,带有嵌套的表单生成器表单组 - Angular 9 “No provider for ControlContainer” VSCode error with a nested Form Builder Form Group 没有ControlContainer AngularJS2的提供程序 - No provider for ControlContainer AngularJS2 在Angular 2模板中使用纯POST表单会导致:“模板解析错误:ControlContainer没有提供程序” - Using a plain POST form within an Angular 2 template results in: “Template parse errors: No provider for ControlContainer” 使用ngControl导致错误:没有ControlContainer的提供程序 - Using ngControl caused error: No provider for ControlContainer 当子组件具有ngModelGroup时,没有ControlContainer的提供者 - No provider for ControlContainer when a child component has a ngModelGroup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM