Encountering `No provider for ControlContainer!` error while utilizing [class.someClass] in Angular 2?

I have a project in progress which involves using angular 2. I am currently working on forms and validation and have encountered an issue. Although I have found a workaround, I am curious to understand why my original code is not functioning properly.

Below is the revised version of my code:

@Component({
    directives: [ DisplayErrorComponent ],
    styleUrls: ["src/modules/component@channel/src/add/add.css"],
    template: `        
        <form (ngSubmit)="onSubmit()" [ngFormModel]="channelForm">
            <label for="link">
                Customize the link
                <div class="link inputBordered inputTexted" [class.error]="!channelForm.controls.link.valid">
                    <span class="static">http://www.mysite.com/mediatheque/</span>
                    <input type="text" name="link" ngControl="link" required>
                </div>
            </label>
            <input type="submit" value="Add" [disabled]="!channelForm.valid">
        </form>
    `
})
export class EditComponent {
    channel: Media;
    channelForm: ControlGroup;
    categories: Category[] = this._categoryService.listAll();

    constructor(fb: FormBuilder, private _categoryService: CategoryService, private _routeParams: RouteParams, private _mediaService: MediaService) {
        this.channel = this._mediaService.getById(this._routeParams.get('id'));
        this.channelForm = fb.group({
            link        : fb.control(this.channel.link, Validators.compose([Validators.required, UrlValidator])),
        });
    }

    onSubmit():void {
        console.log('form submitted')
    }
}

The issue lies in line 8-9 where there is a commented line. Using [class.error]="true" causes Angular to throw an error:

EXCEPTION: Error: Uncaught (in promise): No provider for ControlContainer! (NgControlName -> ControlContainer)

However, when I switch to using [ngClass], everything seems to work fine. I am curious to know why this is the case. (I can provide all the code from edit.component.ts if necessary).

Answer №1

This issue has been highlighted in a similar problem discussed on this link. It is a recognized issue according to this source.

Answer №2

Make sure to replace your code with the following:

<input type="text" name="link" 
  [ngFormControl]="channelForm.controls.link" required>

It's important to use ngFormControl instead of ngControl in this case. This is because ngControl is specifically for inline form declarations, while you should be using FormBuilder to define your link control.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on accessing js file in an Angular application

I have a component where I need to create a function that can search for a specific string value in the provided JavaScript file. How can I achieve this? The file path is '../../../assets/beacons.js' (relative to my component) and it's named ...

Issue with Ionic app causing code execution to hang when the Back Button is pressed

I am currently working on an application using Ionic and React. There is a page in the app where users can upload images from the camera or gallery, which are then saved as binary data in a database (indexed db using Dexie). Everything seems to be function ...

Revamp your AngularJS controller for a fresh new look

How can a controller be refreshed in Angular? Take into account the scenario below: <div ng-controller="MsgCtrl" ng-repeat="m in messages">{{m}}<br></div> <script> var ws = new WebSocket(something, something); function MsgCtrl($sco ...

What is causing JS to malfunction and preventing App Scripts from running `doGet()` when using either `e` or `event` as parameters?

Following a Basic Web App video last night, I meticulously followed every step until the very end where things started to go wrong. Today, I decided to start from scratch and recreate it all. Despite weeks of coding practice, I can't seem to figure ou ...

How can you access the input value from load dash's debounce function in a Vue.js application?

Is there a way to capture the input entered during the @typing event of the b-autocomplete component in Buefy? During the @typing event, the debounce method is called with specific parameters as shown below- <b-field label="Location"> ...

Managing the verification of data existence in the ExpressJS service layer or controller

Working on a medium-sized website, I realized the importance of writing maintainable code with a better project structure. After stumbling upon this insightful article and some others discussing the benefits of 3-layer architecture, I found the concept qu ...

Create a fresh field for the form and store the data in the database

Issue Number One: I am looking to dynamically add a new field along with a button click event that will generate the new field. I attempted to use Jquery for this purpose, but as a newbie in this type of programming language, I am struggling. Can anyone o ...

The module 'PublicModule' was declared unexpectedly within the 'AppModule' in the Angular 4 component structure

My goal is to create a simple structure: app --_layout --public-footer ----public-footer.html/ts/css files --public-header ----public-header.html/ts/css files --public-layout ----public-layout.html/ts/css files In public-layout.html, the stru ...

Sending an array of functions to the onClick event of a button

Having difficulty with TypeScript/JavaScript Currently working with an array of functions like this private listeners: ((name: string) => void)[] = []; Successfully adding functions to the array within another function. Now looking to trigger those ...

Creating a mock instance of a class and a function within that class using Jest

I am currently testing a class called ToTest.ts, which creates an instance of another class named Irrelevant.ts and calls a method on it called doSomething. // ToTest.ts const irrelevant = new Irrelevant(); export default class ToTest { // ... some impl ...

Tips for troubleshooting an Express application launched by nodemon through a Gulpfile in WebStorm 10

I have a unique Express application that is powered by a Gulpfile configuration. gulpfile.js 'use strict'; var gulp = require('gulp'); var sass = require('gulp-sass'); var prefix = require('gulp-autoprefixer'); va ...

How can we showcase an HTML file in Express by utilizing a POST request?

My objective is to successfully submit my form data and then dynamically navigate to an HTML file where the values are displayed. Here's the code snippet from my app.post function in Express: const results = fs.readFile("order.html", " ...

I am facing issues with my filtering functionality on the Angular Typescript mat-table component

I am facing issues while trying to filter my table, the reaction is not correct and I can't seem to find where I went wrong. Here is the HTML part : <mat-form-field appearance="outline"> <mat-label>Search</mat-label> & ...

What is the best way to insert a record into the rth column of the nth row in a table

In the table I'm working with, there are 6 columns and only 5 of them have data filled in. The last column is currently empty for all rows. I am now trying to populate the last column of each row with some data. Can someone guide me on how to use a f ...

Is there a way to automatically fill number fields by clicking on radio buttons using a JavaScript function?

Currently, I am facing an issue with a task that involves three elements: 1. Two Number fields (from standard class) that need to be populated dynamically. 2. A Javascript function designed to extract coordinates using geolocator. 3. A radio button which, ...

Execute local server's unit tests using a Gulp task

I am currently faced with the challenge of running automated unit tests in the terminal for a library application that utilizes the History API internally. To achieve this, I am utilizing Babel to transpile and consolidate my Mocha/Chai/Sinon unit tests in ...

An error occurred when trying to set a cookie using Set-Cookie in a react application

Currently, I am immersed in a small project that involves user authentication via email and password before gaining access to their individual profiles. The backend operates on cookies which are established once the correct email and password combination i ...

Tips for effectively managing TypeScript values with various types

I am currently facing an issue with the TS interface shown below: export interface Item { product: string | Product; } When I try to iterate through an array of items, I need to handle the type checking. For example: items = Items[]; items.forEach(ite ...

"Customizing FusionCharts: A step-by-step guide to changing the background color

Is there a way to modify the background color of fusionchart from white to black? Additionally, how can I change the font color in the chart? https://i.sstatic.net/MMuIq.png ...

What is the correct way to integrate a HTML/CSS/JS theme into a Vue project effectively?

As a newcomer, I recently acquired a bootstrap theme that comes with HTML, CSS, and JavaScript files. My goal now is to integrate this theme into Vue in order to make it fully functional. The challenge I am facing is how to successfully incorporate the the ...