Transmit information to Angular element module

Suppose I have a specific component that I wish to export and utilize as an independent angular-element-component.

export class CompanyFormComponent implements OnInit {

@Input() public companyId: string;
company: Company;
companyForm: FormGroup;

constructor(
    private companyService: CompanyService,
    private fb: FormBuilder,
  ) { }

ngOnInit() {
if (!this.companyId || this.companyId.length === 0) {
  console.log('Please provide an Id');
}

....

In AppModule:

    export class AppModule {
  constructor(private injector: Injector) {}
  ngDoBootstrap() {
    const strategyFactory = new ElementZoneStrategyFactory(CompanyFormComponent, this.injector);
    const element = createCustomElement(CompanyFormComponent, { injector: this.injector, strategyFactory });
    customElements.define('app-company-form', element);
  } 
}

In Index.html:

<app-company-form></app-company-form>

I intend to be able to inject any value wherever I decide to use this component, in a straightforward manner like this:

<app-company-form companyId=11></app-company-form>

This way, the companyId can be populated from any external source, completely outside of Angular. I prefer not to hard code the value within Angular itself.

Although I've experimented with a few solutions, none have proven successful so far.

Answer №1

Customizing your component into an Angular element allows you to include the companyId input parameter by simply adding a specific HTML attribute to your custom element.

<app-company-form company-id="11"></app-company-form>

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

Is NgForm considered a directive within Angular - is it a structural or attribute directive for components?

I'm new to this, so please bear with me. According to https://angular.io/guide/attribute-directives: Angular has three types of directives: Components – directives that come with a template. Structural directives – alter the DOM layout by adding ...

Angular2: Implementing a nested subscription with a secondary subscription within a loop

I am still discovering the world of Angular2, just a week into it! Currently dealing with 2 API calls in my project. The initial API call fetches an array (queryResults) of JSON objects. (1st Subscribe) Showcasing that array in the view. Iterating throu ...

Is it possible to retrieve the form value in Angular that is different from what is displayed?

Is there a way in Angular to retrieve form values differently than how they are displayed? For example, let's say you have an Angular FormInput that displays the value "3,567.56 $" to the user. <input formInputControl="price" money> I want th ...

Managing HTTP requests: A guide to canceling with switchMap

I need to be able to cancel a previous HTTP request if a new one is made to the server, but the current implementation is not working as expected. I want the request to be canceled if a certain value changes in the function. In other words, if the value c ...

Cease the generation of dynamically produced sounds

I am encountering an issue in Angular where I am unable to stop playing an audio from a service. Below is my play() method: play(item: string): void { const audio = new Audio(); audio.src = item; audio.load(); audio.play(); } In order to stop all ...

What could be causing the malfunction of the constructor of these two root services?

Below are two primary root services: Service 1: @Injectable({ providedIn: 'root', }) export class DataService { private data$ = new BehaviorSubject([]); public dataObs$ = this.data$.asObservable(); constructor(private http: HttpClient) ...

What causes the error message "No exported member 'ɵɵFactoryDeclaration' in @angular/core/core" to appear during project compilation?

I am facing an issue where the global Angular CLI version is 13.0.1 and the local version in my project is 10.2.3. Despite making changes to some components (without touching package.json), I encountered an error during the build step of my bitbucket pipel ...

Hide a dialogue box by clicking outside of it

I am working on an Angular application that features a pop-up dialog for user input. I want the dialog to close or hide when the user clicks anywhere outside of it within the main application. This way, the user can input data without interruptions, and th ...

What other configurations are necessary to switch the default port of Angular from 4200 to a different number?

Currently, I am diving into the world of asp.net core webapi and angular 13 through Visual Studio Community 2022. The project setup involves two distinct projects built from these templates: Standalone TypeScript Angular Project Asp.Net Core Web API In ...

How to modify the background color within the mat-menu-panel

Incorporating angular 9 and less into my current project, I have encountered an issue with a mat-menu-panel where my mat-menu-item is located. While I have successfully changed the color of my mat-menu-item, I am now faced with the challenge of changing th ...

Customizing the Android Back Button behavior in NativeScript for a single specific page

I am currently using NativeScript version 5.2.4 along with TypeScript. My goal is to disable the back button functionality in one specific page only, but I'm facing an issue where it also disables the back button behavior for child pages. Below is the ...

What is the best way to restrict a Generic type within a Typescript Function Interface?

Imagine having the following interface definitions: interface SomeInterface {a: string; b: number} interface SomeFunction<T> {(arg: T) :T} The usage of the function interface can be demonstrated like this: const myFun: SomeFunction<string> = a ...

The challenges of handling dynamic controls and reactive forms in Angular when editing, and ways to effectively update their values

I am currently working on creating an inline dynamic form using reactive forms. I have successfully added a new object and looped it in the DOM, but when I press the edit button, it doesn't work and produces errors in the console. Inside the object, t ...

Updating from React 17 to React 18 in Typescript? The Children of ReactNode type no longer supports inline conditional rendering of `void`

When using the React.ReactNode type for children, inline conditional renders can cause failures. Currently, I am utilizing SWR to fetch data which is resulting in an error message like this: Type 'false | void | Element | undefined' is not assig ...

Error message: "Cannot access property 'push' of null in Angular FormArray"

Working with Angular Reactive FormArray to dynamically add multiple inputs upon clicking the "Add" button. Encountering an error message stating "Cannot read property 'push' of null" when attempting to add/push input fields. Considering if ther ...

Adding a new key to a specific position in an array using Angular

After organizing my array, here is what it currently looks like: 0: Object { row: 0 } 1: Object { row: 1 } 2: Object { row: 2 } 3: Object { row: 3 } Now, I need to add a new key to position 2. The updated structure should resemble this: 0: Object { row: 0 ...

Retrieving the return type of generic functions stored in a map

In this unique scenario, I have a dictionary with polymorphic functions that accept the same argument but return different results: const dict = { one: { foo<A>(a: A) { return [a] as const } }, two: { foo<A>(a: A) { ...

Ignore TypeScript errors when using TSNode with Mocha by forcing the compiler to emit code despite errors and having TSNode execute the emitted code

Below is a code snippet where the function test2 is invalid, but it should not affect testing of function test1: export function test1(): boolean { return true; } export function test2(): number { return "1"; } Test: import { assert as Assert } fr ...

Angular site deployed to Firebase Hosting encounters a connection issue with ERR_CONNECTION_REFUSED - could this be due to a

I recently tested an admin template from angular-templates.io and everything was working perfectly fine in the production build. I then uploaded it to Firebase Hosting specifying dist/browser (which was created during the production build). However, upon l ...

Ways to incorporate type into an object comprised of n element in Typescript

Is there a way to assign types to objects with different keys and values, but where "other" remains constant across all of them? How can this be achieved using TypeScript? { color: "red", size: "small", other: { price: 345 discount: 10 ...