Angular component name constraints - 'the selector [your component name] is not permissible'

When trying to generate a component using the Angular 6 CLI (version 6.0.7), I encountered an issue. After typing in ng g c t1-2-3-user, I received an error message stating that the selector (app-t1-2-3-user) is invalid.

I wondered if there was something inherent about this name that was not allowed by Angular. Interestingly, I had previously created a parent module named t1-myModule using ng g module t1-myModule without any issues. The problem seems to be specific to naming a component as t1-2-3-user within that module.

To test this hypothesis, I tried creating a different component with the command ng g c t1-testComponent and it worked perfectly. So, it appears that the CLI is functioning properly, but there may be some restriction on naming conventions for components like t1-2-3-user in Angular.

EDIT: Upon further investigation, I discovered that Angular dislikes having a number as the first character after a dash (-) in a component name. This limitation might be similar to restrictions found in JavaScript variables, considering that Angular eventually compiles into JavaScript. Can anyone provide more insight or confirm this constraint on component naming?

Answer №1

According to the regulations set by W3C standards, selectors must meet the following criteria:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain

  1. only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_);
  2. they cannot start with a digit, two hyphens, or a hyphen followed by a digit
  3. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

Angular adheres to this W3C convention for selector names. A similar validation process is performed within the Angular CLI codebase. The actual implementation involves using a regular expression (

/^[a-zA-Z][.0-9a-zA-Z]*(:?-[a-zA-Z][.0-9a-zA-Z]*)*$/
) to ensure that the specified selector meets the required criteria before creating files.

When executing the

ng generate component component-name
command, Angular calls upon @angular/schematics for the component command and supplies the component name as a parameter. This triggers a validation check on the selector, as demonstrated in the following line of code: below line.

validateHtmlSelector(options.selector);

For further details, refer to validation.ts

export const htmlSelectorRe = /^[a-zA-Z][.0-9a-zA-Z]*(:?-[a-zA-Z][.0-9a-zA-Z]*)*$/;
export function validateHtmlSelector(selector: string): void {
  if (selector && !htmlSelectorRe.test(selector)) {
    throw new SchematicsException(tags.oneLine`Selector (${selector})
        is invalid.`);
  }
}

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

Angular - CSS Grid - Positioning columns based on their index values

My goal is to create a CSS grid with 4 columns and infinite rows. Specifically, I want the text-align property on the first column to be 'start', the middle two columns to be 'center', and the last column to be 'end'. The cont ...

Troubleshooting: Inability to Use Type Assertions while Retrieving Data from

Struggling to retrieve and analyze complex data from Firebase for computation and Cloud Function execution. The format of the data is not aligning with my needs, as shown in this example: interface CourseEvent { coucourseGroupType: string; date: Fireb ...

Encountering a GitHub REST API CORS issue while attempting to fetch a public repository's git archive

I'm currently working on developing an Angular application that will be hosted on my GitHub pages using a custom verified domain. Below is the code I am using to call the GitHub API in order to obtain the zip archive of one of my (public) repositori ...

What is the best method to terminate an Electron application using TypeScript?

I have been searching for the proper method to close an Electron app. My app uses React and TypeScript. After coming across this helpful post, I discovered a working solution: const remote = require('electron').remote; let w = remote.getCurrentW ...

Issue in Angular2: Unable to load the templateUrl file for a component

I encountered an Unhandled Promise rejection: Failed to load error while working on my Angular 2 project: This is the snippet from my messages.component.js file: import { Component, OnInit } from "@angular/core" @Component({ moduleId: module.id, ...

Change the default icon in mat-stepper angular material

I'm attempting to customize the default edit icon in Angular Material's stepper component, but my attempts have been unsuccessful. Here is what I have tried: <mat-horizontal-stepper [linear]="isLinear" #stepper> <mat-step> ...

Managing the token prior to the page loading: best practices

Implementing JWT for authentication without a login page has posed some challenges for me. The issue arises when the site loads and immediately starts with the GET methods to load content before authenticating and saving the token in local storage. The pr ...

Error with Angular 2 observables in TypeScript

When a user types a search string into an input field, I want to implement a debounce feature that waits for at least 300 milliseconds before making a request to the _heroService using HTTP. Only modified search values should be sent to the service (distin ...

Injecting Dependencies with Angular 2 and the Ability to Include Optional Parameters

One issue I'm facing is that I have multiple components in my Angular 2 application that require the same dependency. This specific dependency needs a string for the constructor. How can I instruct angular2 to use a specific instance of this type for ...

When an additional data stream is introduced, Resolver is unable to effectively resolve the issue

I have been working on implementing a resolver to fetch data based on the parameters provided by the route. However, I encountered an issue where the resolver does not resolve when there is an additional data stream that my data depends on. When I direct ...

Is there a way to utilize an Angular Material checkbox without any extra gaps or spacing?

Currently, I am working with Angular Material and trying to figure out how to remove the default spacing around the checkbox. The checkboxes in Angular Material are typically surrounded by some space (as shown in the image). Is there a simple way to elimin ...

Is there a way to combine compiling TypeScript and running the resulting .js file into one build command in Sublime Text 3?

I have successfully installed the TypeScript plugin on Sublime Text 3. Once installed, a build system is added to the menu for easy access. https://i.stack.imgur.com/m21bT.png You can simply press "Command + B" to build a .ts file. My goal is to compile ...

Navigate to the logout page upon encountering an error during the request

I recently upgraded our application from angular 2 to angular 5 and also made the switch from the deprecated Http module to the new HttpClient. In the previous version of the application, I used the Http-Client to redirect to a specific page in case of er ...

Encountered an issue when attempting to send data using this.http.post in Angular from the client's perspective

Attempting to transfer data to a MySQL database using Angular on the client-side and Express JS on the server-side. The post function on the server side works when tested with Postman. Here is the code snippet: app.use(bodyParser.json()); app.use(bodyPa ...

Icon appearing outside the button instead of inside

Here's a button I'm working with: <button class="glyphicon glyphicon-option-horizontal" (click)="myFuncion()"></button> Recently, I updated my bootstrap library from version 3 to 4.2.1 and now the icon is no longer visible. I' ...

Broadcast the latest N occurrences

I am working on an Angular 6 application where I want to display the most recent N events from a continuous stream of events coming from a web-socket. Currently, the data is shown using RxJS Observable<Event[]>: <div *ngFor="let event of (wsEven ...

Angular 8 - Unraveling the Mystery Behind its Initial Page Discovery

I am facing an issue with my Angular 8 application where the login page is being displayed at startup. I have checked the routing module but couldn't find anything that explicitly sets this page as the startup page. Any suggestions on what might be c ...

What is the reason behind TypeScript requiring me to initialize a property even though I am retrieving its value from a local reference?

I am just beginning to explore Angular. This is the template for my custom component: <div class="row"> <div class="col-xs-12"> <form action=""> <div class="ro"> <d ...

A guide to extracting attribute values from HTML using Angular 4

I am currently working on an Angular 4 project where I needed to implement drag and drop functionality. To achieve this, I utilized the ng2-dragula plugin. Now, I have a new requirement which involves extracting the data-id attribute from each row after it ...

Is there a way to access and read an Excel file stored within the assets folder using Angular?

I need assistance converting an excel file to a JSON format. My excel file is currently stored in the assets folder, and I am looking for guidance on how to access it from app.component.ts. Any help would be greatly appreciated! ...