102

I have my custom component:

@Component({
    selector: 'my-custom-component',
    templateUrl: './my-custom-component.html',
    styleUrls: ['./my-custom-component.css']
})
export class MyCustomComponent {
    constructor() {
        console.log('myCustomComponent');
    }
}

I can use it like this:

<my-custom-component></my-custom-component>

But how I can pass a variable? For example:

<my-custom-component custom-title="My Title"></my-custom-component>

And use this in my component code?

2
  • Using an input in your component: angular.io/docs/ts/latest/guide/… Commented Feb 17, 2017 at 0:36
  • Just a note: there are 4-5 ways to do so. In this example that's a parent/child relationship, therefore it's easy and Input would work. However, for components without relation a service is required, take a look into the blog post fireship.io/lessons/… Commented Sep 19, 2020 at 16:41

2 Answers 2

169

You need to add Input property to your component and then use property binding to pass value to it:

import { Component, Input } from '@angular/core';

@Component({
    selector: 'my-custom-component',
    templateUrl: './my-custom-component.html',
    styleUrls: ['./my-custom-component.css']
})
export class MyCustomComponent {

    @Input()
    customTitle: string;

    constructor() {
        console.log('myCustomComponent');
    }

    ngOnInit() {
        console.log(this.customTitle);
    }
}

And in your template:

<my-custom-component [customTitle]="yourVariable"></my-custom-component>

For more info, check out this page.

Sign up to request clarification or add additional context in comments.

8 Comments

this work! Any idea why customTitle variable is undefined in constructor() ?
@rpayanm It's undefined because Input is Angular's decorator and dit "lives" through Angular's lifecycle, therefore it's only available in OnInit and after (constructor doesn't have anything to do with Angular, it is initialised before OnInit). You can read more about Angular's lifecycle hooks here: angular.io/docs/ts/latest/guide/lifecycle-hooks.html
but how can access string "myAnotherVariable" ? <my-custom-component [customTitle]="yourVariable">myAnotherVariable</my-custom-component>
why potentially customTitle could be "undefined"? because I receive this
@AnnaLeonenko maybe you specified a static value with [customTitle]="yourLitteralString" instead of customTitle="yourLitteralString"
|
27

You can add an @Input() decorator to a property on your component.

export class MyCustomComponent {
    constructor() {
        console.log('myCustomComponent');
    }

    @Input() title: string;
}


<my-custom-component title="My Title"></my-custom-component>

or binding title from a variable 'theTitle'

<my-custom-component [title]="theTitle"></my-custom-component>

See the @Input()decorator documentation.

1 Comment

BTW, your @Input element may need to be declared public for it to work properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.