What is the correct way to pass parameters to Angular components?

I've been struggling with a problem in Angular for days now as I'm new to it. The issue is related to passing parameters in a component where the values are not displaying correctly.

Initially, I attempted to pass parameters like this:

<app-pr1-button class="grid-item grid-item-1"
                [name]="'name1'"
                [label]="'label1'"
                [text]="'text1'"
                [inputtext]="['m1','m2']"
>
</app-pr1-button>
    <app-pr1-button class="grid-item grid-item-2"
                        [name]="'name2'"
                        [label]="'label2'"
                        [text]="'text2'"
                        [inputtext]="['n1','n2']"
        >
</app-pr1-button>

While the 'name' parameter was displayed correctly in both components, the others were showing the same values from the first component due to styling implications (done using SCSS).

Subsequently, I tried an alternative method of passing parameters:

<ng-template #temp>
    <h1>label1</h1>
    <p>text1</p>
    <app-input-data [text]="['m1','m2']"></app-input-data>
</ng-template>
<app-pr1-button class="grid-item grid-item-1" [name]="'name1'" [template]="temp"></app-pr1-button>

<ng-template #temp1>
    <h1>label2</h1>
    <p>text2
    </p>
    <app-input-data [text]="['m1','m2']"></app-input-data>
</ng-template>

<app-pr1-button class="grid-item grid-item-2" [name]="'name2'" [template]="temp1"></app-pr1-button>

The challenge lies in updating data when using SCSS styles. Any suggestions on how to tackle this issue would be greatly appreciated.

P.S. Below is the snippet of SCSS used:

@import url('https://fonts.googleapis.com/css?family=Raleway:300,400,600&subset=latin-ext');

$main-color: #9191E9;

* {
  box-sizing: border-box;
}

html, body {
  font-family: 'Raleway', sans-serif;
  font-size: 16px;
}
...

Answer №1

Implementing component parameters follows a consistent pattern:

  1. Add an Input property declaration in the component's code file: @Input() prop: string;
  2. Assign the value in the markup where the component is placed:
    <my-component [prop]="'Hello'"></my-component>

From what I can see in your example, you seem to be on the right track.

However, there are some aspects of your code that may cause confusion, such as passing a TemplateRef as an input property or using ViewContainerRef without clear intent. My recommendation is to focus on your goal, refactor as needed, and everything should fall into place.

Answer №2

My mistake was quite silly

Initially, I made this change:

<a class="button" href="#popup">{{name}}</a>
<p class="longread">{{longread}}</p>
<div class="popup" id="popup">

But it should have been changed to:

<a class="button" href="{{'#'+name}}">{{name}}</a>
<p class="longread">{{longread}}</p>
<div class="popup" id="{{name}}">

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

What is the best way to verify that I am receiving the 'jwt' token in my code?

Trying to understand the data held by the jwt token in my next.js app, but encountering an error message saying error: jwt must be provided. Here's the code snippet causing the issue: import { NextRequest } from "next/server" ...

Substitute the division

Can I make the old div change its position and move to the side when I add a new div? And can all the old divs be hidden when adding four new divs? This is for my full news website and I want this applied to all four pages. First page: https://i.sstatic. ...

angular handling backend post API responses

Looking for suggestions on how to consume a post API that produces JSON events in Angular. EventSource does not support post requests, so I'm new to Angular and seeking alternative ways to handle these events. Any advice or recommendations would be gr ...

Is there a way for me to manually manipulate the advancement of the progress bar from @ngx-progressbar/core in Angular5/Ionic4?

I've been working on implementing a progress bar into my application using the @ngx-progressbar/core library. However, I'm facing an issue where I can't seem to control its progress effectively. Whenever I try to increase the progress increm ...

What is the CSS method for determining the distance from the top of a container to the edge of the window?

I am working on a basic HTML layout that contains a few elements, including a scrollable div container located below them. Since the height of unknown-height is uncertain due to dynamic element generation, I need a simple method to enable scrolling in the ...

What is the significance of using an empty @Injectable() decorator in Angular?

My experience with Angular development is not consistent. One thing I have come to understand about the @Injectable() decorator is that it signifies - this class can receive injected dependencies and not this class can be injected as a dependency I can ...

Pictures are floating to the side instead of being centered in mobile mode

When in desktop mode, one img is aligned to the left and the other one is aligned to the right. However, when viewed on a phone, I am attempting to center align them but they continue to be aligned to the sides. What could be the issue here? Provided be ...

Responsive CSS Circle Design

Currently, I am using the Bones WP theme as a starting point. Within a 3 column layout, I have three circles placed (one in each column). The structure of each circle is as follows: HTML <a href="#" class="services">Text</a> ...

Styling forms with HTML and CSS: tips for proper positioning

My webpage contains two forms that are functioning correctly, but due to the order of the HTML code, the second form appears beneath the first. I am looking to display them side by side or in specific positions on the page. Is there a way for me to contr ...

Step-by-step guide to mocking or spying on an imported function during Angular unit testing

Imagine you have an Angular 6 component with a method test that returns a certain value: import { doSomething } from './helper'; @Component({ ... }) export class AppComponent { test() { const data = doSomething(1); retur ...

Typescript signals that the symbol being exported is not recognized as an exported symbol

I am working on a straightforward project $ ls -l total 32 -rw-rw-r-- 1 ocket8888 ocket8888 72 Apr 29 09:30 index.ts -rw-rw-r-- 1 ocket8888 ocket8888 105 Apr 29 09:31 main.ts drwxrwxr-x 4 ocket8888 ocket8888 4096 Apr 29 09:26 node_modules -rw-rw-r-- 1 o ...

The integration of jQuery Masonry with margin-right for a seamless and

I have a row with two columns, where the first column has a right margin and the second one does not. I would like to use jQuery Masonry to eliminate any empty spaces. However, it seems that the margin right does not interact well with Masonry. Is there a ...

Working with Angular forms: Adding an element to a specific index in a

addStop() { const control = <FormArray>this.editForm.controls['stops']; control.push(this.initStop()); } I have a function that adds a new "stop" to the form array. However, I would like to add the new "stop" one position before th ...

What steps do I need to follow in order to set the language of the npx bootstrap datepicker to pt

I have been working on a project using Angular 4, where I incorporated ngx bootstrap for date picker functionality. However, I encountered an issue with the date picker being displayed in English. In an attempt to rectify this, I added the following lines ...

Exploring the growth of CSS offsetting alongside with AngularJS

I am working on an Angular app where I need to increase the left offset of a div by 90px every time the slideThis() function is called. In jQuery, I would typically achieve this using left: '+=90', but I'm struggling to find a similar method ...

Exploring the utilization of CSS host selectors with Angular4 to target direct child elements

Within my app.component.css file, I currently have the following code snippet: * ~ * { margin-top: 24px; } This rule actually adds some top margin to all elements that come after the first one. However, this is not exactly what I intend to achieve. My ...

Different ways to separate an axios call into a distinct method with vuex and typescript

I have been working on organizing my code in Vuex actions to improve readability and efficiency. Specifically, I want to extract the axios call into its own method, but I haven't been successful so far. Below is a snippet of my code: async updateProf ...

Issue with Angular Subscribe functionality when using API service

While attempting to connect to an API using a service I created in Angular, I encountered an error in the browser console: main.ts:5 ERROR NullInjectorError: R3InjectorError(Standalone[_HomeComponent])[_EventosService -> _EventosService -> _EventosSe ...

Troubleshooting border problems with Bootstrap 3 tables

Recently, I encountered a problem with the Bootstrap 3 table-bordered where the right border was not showing up. Below is the code snippet of my table: <table class="table table-bordered table-hover"> ... </table> Upon inspecting the table vi ...

Developing a responsive navigation bar for mobile devices

Can anyone help me with creating a mobile navbar that shows the hamburger icon on smaller screens? I would like the links to appear in blocks when the icon is clicked. I attempted to make it using an SVG icon for the hamburger and setting the display to n ...