Understanding how to access an application selector's property during initialization

This is the initial page, generated on the backend (the json-configuration will replace 'propval' as a variable):

<%- include('template/head'); %>
<application [userdata]='"propval"'>
    Loading...
</application>
<%- include('template/foot'); %>

Here is the component that manages the 'application' selector:

@Component({
    moduleId: module.id,
    selector: 'application',
    templateUrl: './app.component.html',
    providers:[
        ProgressBar
    ]
})
export class AppComponent implements OnInit {

    public userdata;

    constructor(public progressBar :ProgressBar, private router: Router) {
        console.log("constructor",this.userdata);
    }

    public async ngOnInit() {
        console.log("ngOnInit",this.userdata);
    }

}

When looking at the console, the output shows:

constructor undefined
ngOnInit undefined

How can we access the userdata property? It seems like this might not be achievable in the AppComponent, since the application selector is not rendered within the app.component.html. The rendering of the application selector occurs on the backend.

Answer №1

Vega raised an interesting question in a discussion regarding the limitations of input bindings with bootstrap and entry components. The absence of source code suggests that entry components, being detached from parent components, do not support input bindings.

The solution proposed in the accepted answer involves using ElementRef to retrieve data attributes, a method which should prove effective in your scenario as well.

import {ElementRef} from '@angular/core';

constructor(private _ref: ElementRef) {}

ngOnInit() {
    this.userdata = this._ref.nativeElement.getAttribute('userdata');
}

For a hands-on example, check out the sample plunker.

Consider exploring other solutions like utilizing an InjectionToken (details here) or leveraging a service for alternative approaches.

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

The state of NgRx is consistently in an undefined state

I'm having an issue with my state always being undefined in NgRx, can someone help me out? reducers.ts import * as actions from "./actions" import * as _ from "lodash" export interface State { cards: any[] } export const initialState: State = { ...

Having trouble implementing conditional rendering for components sourced from a .json/.ts file in Angular

Presented below is the JSON file which includes a .text component with a text field and a .radio component featuring a radio button. How can we efficiently display them conditionally based on the content of the .json file? Here's the contents of the ...

Incorporating map, forkJoin, and mergeMap into your code base can

I have a requirement to perform multiple API calls. The first API call returns a list of objects with the properties userId and propertyId. For each item in this list, I need to fetch additional information called userInfo and propertyInfo based on the I ...

Issue: subscribe function is not definedDescription: There seems to be an

I'm currently trying to retrieve a value from an array based on a specific element, and then finding the exact matching element. Unfortunately, I've encountered an error with this.getProduct(name1: string) function even though I have already impo ...

Updating the background color using typescript

Before transitioning to angular development, I had experience working with vanilla Javascript. I encountered a challenge when trying to modify the css properties of specific elements using Typescript. Unfortunately, the traditional approach used in Javascr ...

After subscribing, creating the form results in receiving an error message that says "formgroup instance expected."

I am currently working on a project using Angular 6 to create a web page that includes a form with a dropdown menu for selecting projects. The dropdown menu is populated by data fetched from a REST API call. Surprisingly, everything works perfectly when I ...

Saving data from rowclicked event in ag-grid with Angular

My goal is to save the event data from the onRowClicked event in a Component member. This way, when the user clicks a button, the data can be deleted. However, I am facing an issue where the member variable becomes undefined when trying to access it in t ...

"Encountering a problem during the installation of the Angular

After investing numerous hours, I am still unable to identify the issue with running an angular based project. node version: v12.16.1 I executed npm install -g @angular/[email protected] However, upon entering the command ng build --prod, I enco ...

Updating parent components through child components in ReactWould you like another unique

In my current project, I am attempting to change the state of the main component labeled App.tsx by using a child component called RemarksView.tsx. I have attempted passing props such as setRemarks and remarks, but unfortunately the state in the parent c ...

When you switch to a different URL within the same tab, the session storage will be automatically cleared

My current web application is experiencing an issue with session storage. Whenever I navigate to a different URL within the same tab, it seems like the session storage is being cleared. Let me walk you through the situation: I initially store some data ...

Testing child components with the @output directiveWould you like to learn

Exploring the testing of a child component's @output feature in Angular 2 is my current goal. My aim is to utilize a mock version of the child component's @output functionality to trigger a function within the parent component for testing purpose ...

As 'include_docs' cannot be utilized in fetchRevs due to its absence in both the BulkFetchDocsWrapper interface and the DocumentFetchParams interface

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/bb1cc0e143f40f52a8d771e93036fc211df85cfb/types/nano/index.d.ts#L160 I'm a novice when it comes to working with CouchDB. I'm aware that I can use "fetch" to get a document in the result, but ...

Tips for incorporating Angular2 into Eclipse using TypeScript

Recently, I delved into the world of Angular 2 and noticed that TypeScript is highly recommended over JavaScript. Intrigued by this recommendation, I decided to make the switch as well. I came across a helpful guide for setting up everything in Eclipse - f ...

Is there a method to iterate through dynamically added nested arrays, similar to the Russian Matryoshka dolls?

I am facing a challenge in my Angular TypeScript class where I need to define a function that can loop through dynamically added nested arrays. The issue arises because my template displays a custom Kendo grid which requires me to traverse a type of "Matri ...

[Vue warning]: The property "text" was accessed during rendering, however it is not defined on the current instance using Pug

Looking for some guidance from the Vue experts out there. I've recently started working with Vue and I'm attempting to create a view that verifies an email with a unique code after a user signs up. Right now, my view is set up but it's not c ...

ErrorHookWebpack: When executing npm build in the node container, the service is detected as inactive

Encountering an issue when executing npm run build within a container on Kubernetes during a gitlab-ci job. It's worth noting that npm install, npm run lint, and npm run test all completed without any problems. Error: node@runner-7gbsh-sz-project-966 ...

Having Trouble with Ionic 2's Loading Controller

In my attempt to utilize the recently added LoadingController in this scenario: let loading=this.load.create({ content: "Connexion au serveur Migal en cours..." }); loading.present(); this.http.get(this.urlCheckerForm.value.migalUrl+'/action/Mobi ...

Triggering the detection of changes even when the value linked to remains the same

I'm facing an issue with a component that is supposed to react to changes in input controls by reformatting the input and removing certain characters. If the removed character corresponds to the previous value, the component fails to detect any change ...

Struggling to make Typescript recognize the css prop (emotion) when styling Material-UI components

I'm on a mission to set up a Typescript project with Material-UI v4.11.4 and implement emotion for styling in anticipation of the MUI v5 release. The aim is to integrate emotion into the project so developers can transition to using the new styling in ...

Tips for creating a seamless merge from background color to a pristine white hue

Seeking a seamless transition from the background color to white at the top and bottom of the box, similar to the example screenshot. Current look: The top and bottom of the box are filled with the background color until the edge https://i.stack.imgur.com ...