Unable to locate "angular", attempting to retrieve and validate values from the HTML form

I'm relatively new to Angular and I'm looking to utilize angular.module('FooApp', []); in order to fetch HTML form data and conduct validation within my .ts component file.

Unfortunately, it appears that "angular" isn't being recognized in my IDE (Visual Studio Code). I'm uncertain about the necessary library required for this task.

I'm attempting to follow this example (Scroll down to Custom Validation): https://www.w3schools.com/angular/angular_validation.asp

HTML:

<form [formGroup]="infoForm" (ngSubmit)="validateForm()" my-directive>

  <label>First Name </label> <input type="text" maxlength="25" name="first" 
autofocus required /><br><br>
  <label>Last Name </label> <input type="text" maxlength="25" name="last" 
required /><br><br>
....
</form>

<script src="data-form.component.ts"></script>

.ts File:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

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

export class DataFormComponent implements OnInit {

  infoForm: FormGroup;

  // Form values for a user
  constructor(private formBuilder: FormBuilder) {
     this.infoForm = this.formBuilder.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      ....
    })
 }

  clearForm() {
    // Reset form names to null for clearing form
  }

  // Incomplete Implementation, encountering an error here
  validateForm() {

    // The issue lies here
    var app = angular.module('FooApp', []);

    if (this.infoForm.invalid) {
      return;

    }
  }

  ngOnInit() {
  }
}

The Error:

https://i.sstatic.net/5JR0R.png

Cannot find name, "angular".

My project structure:

https://i.sstatic.net/HjGyC.png

To better understand the environment I'm working in, watch this video https://youtu.be/5wtnKulcquA?t=372

Answer №1

You have a mix of AngularJS and Angular in your code, as the comments point out.

Here is an example from your code:

if (this infoForm.invalid)

Currently, you are checking if the form is invalid. If you want to check each control individually, you can access them through 'infoForm':

this.infoForm.controls.firstName.invalid

For more information on this.infoForm.controls.firstName, refer to this image: Debug information

A control is of type FormControl, allowing you to determine its validity, touch status, dirtiness, pristineness, etc.

Refer to the Form Control official documentation for further details.

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

Troubleshooting a Bug in Angular Component with requestAnimationFrame

Using a combination of Angular 9 and THREE.Js, I have created an app that allows users to switch between a 2D html component and a 3D ThreeJs component with the click of a button. The 3D component initializes all necessary ThreeJs elements on ngAfterViewIn ...

Collect the GET parameters as an array of strings when there is only one value

How can I properly pass a string array as a parameter for a GET call? // Passing one value param: filters=Something value: filters: 'Something' // Passing multiple values param: filters=Something&filters=Something else value: filters: [ &ap ...

What is the process for incorporating a custom action in TestCafe with TypeScript?

I've encountered an issue while trying to implement a custom action. When running tests with the new custom action, I keep receiving an error stating that my custom action is not recognized as a function. TypeError: testcafe_1.t.customActions.selectFr ...

Is there an external class available to integrate with my interface?

Is there a method to establish an external class (part of an NPM module) as a class that adheres to a specific interface? Consider the following scenario: import {someClass} from "someNPMmodule"; interface myInterface { foo: () => void } I am now ...

Exploring Angular Route Configurations: Utilizing Multiple Outlets with Path as an Array of

In my Angular9 application, I have configured hundreds of routes paths. Is there a way to use multiple outlets with a single array of string paths? Current Code: const routes: Routes = [ { path: 'Data/:EntityID/:Date', compon ...

The issue with npm modules not appearing in EMCA2015 JS imports persists

I am currently in the process of developing a mobile application with Nativescript using the Microsoft Azure SDK. To get started, I installed the SDK via npm by running this command: $ npm install azure-mobile-apps-client --save However, upon attempting ...

Transforming the DateandTime into a time format and cross-referencing it with the database for

I am trying to implement a validation process that checks if the combination of CCN and date already exists in my database. The dates are saved in "yyyy-MM-dd HH:mm:ss" format, but I only want to compare the date portion [yyyy-MM-dd] in my validation. My ...

Generate PDF from HTML in Angular 5 after invoking several services

Currently facing an issue with Angular version 5. I have a parent page which is a review page, and 5 component pages as children. Whenever I navigate to the review page, it automatically takes a snapshot by exporting the HTML to PDF. However, the 5 compone ...

What advantages do binary shifts offer in enums?

What do you think about this code snippet? /** * Bitmask of states */ export const enum ViewState { FirstCheck = 1 << 0, // result is 1 ChecksEnabled = 1 << 1, // result is 2 Errored = 1 << 2, // result is 4 ...

Next.js components do not alter the attributes of the div element

I am encountering a problem with nextjs/reactjs. I have two tsx files: index.tsx and customAlert.tsx. The issue that I am facing is that the alert does not change color even though the CSS classes are being added to the alert HTML element. Tailwind is my c ...

Angular: Dynamically add or delete an interceptor based on conditions

Is it possible to dynamically include or exclude an interceptor based on user selection? For my application, I want to enable Azure AD based SSO using the @azure/msal-angular package https://www.npmjs.com/package/@azure/msal-angular that provides an inter ...

The property 'matCellDefTrackBy' cannot be bound to 'mat-cell' as it is not recognized as a valid property

Wondering how to implement trackBy in Angular Material? I encountered the error message below: Can't bind to 'matCellDefTrackBy' since it isn't a known property of 'mat-cell' html: <mat-table [dataSource]="data" ...

What is the best way to dynamically assign formControlNames in Angular using *ngFor?

I am currently facing a challenge with setting form controls using *ngFor over objects in an Array. The number of objects in the array can vary, sometimes resulting in only 1 object while other times multiple objects are present. My specific issue revolve ...

Is it necessary to only override the monospaced font?

Can the monospace font in Angular Material be customized for just the <code>foo</code> blocks? I want to use a font where the number zero 0 looks distinct from the letter oh O. ...

Two trigger functions in Angular animations are not functioning as expected

After updating to the latest version of Angular and starting a new project, I encountered an issue with using two trigger() functions for animations. When attempting to use both trigger() functions, the second one would not work. However, disabling the fir ...

Create a unique TypeScript constant to be mocked in each Jest test

Having difficulty mocking a constant with Jest on a per test basis. Currently, my mock is "static" and cannot be customized for each test individually. Code: // allowList.ts export const ALLOW_LIST = { '1234': true }; // listUtil.ts import { ...

Apply a dynamic function to assign a background color to a specific class

Currently, I have a function called getBackground(items) that returns a background color from a JSON file list. Within my application, there is a component that automatically adds a class name (.item-radio-checked) when a user clicks on an item in the list ...

Enabling Cross-Origin Resource Sharing (CORS) in Laravel and Angular

I am in the process of developing a Laravel web application that will serve as an API for an Angular2 application. Currently, I have the Laravel application hosted on a WAMP server on Windows with Laravel running on localhost:8000 and Angular on localhost: ...

Tips on adjusting the Leaflet Map's zoom level to display all markers in React Leaflet

I am currently working on a project with React Leaflet map that requires changing the center and zoom based on a set of markers. The goal is to adjust the zoom level so that all the markers are visible on the map. To achieve this change in view, I am util ...

Angular Material design failing to display properly

Despite following the Angular Material guide on their website, I am still unable to see the gestures and design properly. I have meticulously followed all the steps outlined in the Angular Material guide. HTML <mat-radio-group> <mat-radio-but ...