Tips for sending information to a nested Angular 2 attribute component

As per the instructions found on this blog, in order to create inner components within an SVG using Angular 2, we need to utilize an [attribute] selector:

// Within svgmap.component.ts file: component declaration
@Component({
    selector: '[svgmap]',
    templateUrl: 'app/svg/map/svgmap.template.html'
  })

In my current setup, the SVGMapComponent is implemented in various parent component templates like so:

    <svg [attr.class]="mapClass[0]" svgmap="1"></svg>
    <svg [attr.class]="mapClass[1]" svgmap="2"></svg>
    <svg [attr.class]="mapClass[2]" svgmap="3"></svg>

While this functions well up to this point, I now find myself needing to access the value passed as the svgmap attribute (e.g. "1,2,3" in the aforementioned example) within the code of svgmap.component.ts, but I am unsure if this is achievable or how to go about it.

If extraction is possible, what syntax should be utilized within my TypeScript child component (i.e. svgmap.component.ts)? If extraction is not feasible, what limitations are preventing it? Is there a workaround available?

P.S.: After exploring resources like this post, while insightful in certain scenarios, it does not directly address the specific situation described above.

Answer №1

To accomplish this, you can follow the same approach as using a directive: simply utilize an @Input() with matching selector name

@Component({
  selector: '[svgmap]',
  templateUrl: 'app/svg/map/svgmap.template.html'
})

export class SvgMapComponent {
   @Input('svgmap') data:number;
}
ngAfterContentInit() {
   // The value of data is now set
   console.log(`SvgMapComponent ngAfterContentInit: level=${this.data}`);
   this.svgClass= `map map--${this.data}`; // An example showing how to use the value
}
// Additional code can be placed here 
} // end of class 

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

When null is assigned to a type in Typescript, it does not result in an error being triggered

Could someone enlighten me on why this code is not causing an error? import { Injectable } from '@angular/core'; interface Animal{ name: string; } @Injectable() export class AnimalService { lion: Animal = null; constructor() {} get(){ ...

Is there a way to denote a specific part of a generic type without explicitly specifying the parts as generics themselves?

My dilemma involves an object defined by a type from a 3rd party library: // Unable to modify this - it belongs to the 3rd party library; interface TypedEvent< TArgsArray extends Array<any> = any, TArgsObject = any > extends Event { args ...

Access the array values by their respective keys in an object that is returned from a custom JavaScript file utilizing the Node.js file system

I recently came across a config file with a unique format, as shown below: define([], function () { return { productItems: { item1: ['Apple', 'Ball', 'Car'], item2: [&apo ...

Encountering an error with "unexpected token import" while utilizing localize-router in an Angular 4

I am currently working on building an Angular 4 app with server-side rendering and language-specific route paths. I am using Angular CLI version 1.5.0-rc1 for this project. While everything seems to be functioning fine, I am facing a problem with incorpor ...

The type 'undefined' cannot be assigned to type 'CartItem'

While running my program, I encountered the error 'Type 'undefined' is not assignable to type 'CartItem'. Unfortunately, I am unable to resolve this issue :(. import { Injectable } from '@angular/core'; import { CartItem ...

Even after setting the handler to return false, Angular continues to submit the form

In the following scenario, I have encountered an issue: Here is the HTML template snippet: <form [action]='endpoint' method="post" target="my_iframe" #confirmForm (ngSubmit)="submitConfirmation()"> <button type="submit" (click)="conf ...

Receiving an error when triggering an onclick event for a checkbox in TypeScript

I am creating checkboxes within a table using TypeScript with the following code: generateTable(): void { var table = document.getElementById("table1") as HTMLTableElement; if (table.childElementCount == 2) { for (var x = 0; x < 2; x++) ...

What is the reason behind the return type of 'MyType | undefined' for Array<MyType>.find(...) method?

Currently, I am in the process of developing an Angular application using TypeScript. As part of this project, I have defined several classes along with corresponding interfaces that align perfectly with their respective properties: Map: export class Map ...

Uploading images using the power of Angular and Node.js

I have been struggling with a persistent issue for quite some time now, and I have not been able to find a solution anywhere. My goal is to allow users to update their avatars on their profiles. However, every time I attempt to pass the file to my node ser ...

Combine all the missing observables in RxJS into a single array

In my code, I am creating a NavBar with items that may require fetching extra information from an API and adding it to the subtitle field. I want to transform this into an Observable<NavItem[]> so that it can be rendered using an Async Pipe. Curren ...

Make sure to confirm that 'tables-basic' is an Angular component within the module before proceeding

In my table-basic.component.ts file, I declared 'tables-basic' as a selector and included this template in dashboard.html. Despite following the steps outlined below, I encountered an error which is also highlighted. Snippet from my dashboard.te ...

Tips for adjusting the material ui Popper width to fit the container without disabling the portal

Currently utilizing the material-ui popper library. I am trying to allow the popper to extend outside of its container in the vertical direction. To achieve this, I have set disableportal={false}. However, upon setting disableportal to false, when assign ...

Running multiple versions of the Angular CLI on one machine

In my various Angular2 projects, I am faced with the challenge of working with different versions of angular-cli. This means that in order to run and compile each project for production, I need to ensure that the correct version of angular-cli is being use ...

Examining interconnected services' dependencies

Looking to test out AService, which has dependencies on BService and CService. The dependency chain looks like this: AService --> BService --> CService The constructor for AService is as follows: constructor( private bService: BService ) {} The const ...

Master the Art of Scrolling Lists in Ionic 2

I am currently using Ionic2 for my project. One of the challenges I'm facing is scrolling to the top of a list when a specific event, called messageSend, occurs. Let me show you the code for this: <ion-content padding class="messages-page-conten ...

Is ngOnDestroy in Angular 2 triggered on page refresh, or only when navigating away from the component?

Can anyone clarify whether the ngOnDestroy method in Angular 2 is triggered on page refresh or only when navigating away? ...

Defining the structure of an object using a type interface

I am having trouble identifying the issue with this type declaration in relation to the interface. When using TypeScript, I encountered an error message stating: "State is an unresolved variable". Does anyone have insights on why this might be considered ...

<T extends object>(value: T): T, but with the type changing from null to string

I discovered a tool called pathmirror that transforms objects like: {a: {b: null} } to {a: {b: 'a.b'} This is particularly useful for naming Redux actions. I'm wondering how I can create a type definition for this? Currently, my declarat ...

I am looking to generate an array with key-value pairs from an object that will be seen in the examination

I have a task that involves configuring a table and creating objects with key-value pairs for each key. type KeyValuePair<T> = {/** ... */}; let userKeyValuePair :KeyValuePair<{id:number,userName:string}>; // => {key:'id',value: ...

Retrieving the value from the series object and showing it in the tooltip of a high chart with the help of Angular 4

I have successfully implemented the display of x and y values in the tooltip of a Highchart within my Angular 4 application. By utilizing the formatter function of the tooltip, I am able to achieve this functionality. The graphdata array is initially set w ...