2

How can I set dynamically the input attribute of my subcomponent with one way binding in angular 4?

For example I have following subcomponent:

@Component({
    selector: 'app-page-header',
    templateUrl: './page-header.component.html',
    styleUrls: ['./page-header.component.scss']
})
export class PageHeaderComponent {
    @Input() heading: string;
}

If I am using this subcomponent in my component as follows it works:

<app-page-header [heading]="'Hello world'"></app-page-header>

But I want dynamically set the heading attribute of my subcomponent like this

<app-page-header [heading]="{{myfield}}"></app-page-header>

or even with pipes

<app-page-header [heading]="{{ 'MY_TEXT_FIELD' | translate:lang }}"></app-page-header>

But it does not work.

How can I achieve this and how is the syntax? Thank you in advance for your answers.

1 Answer 1

2

Just remove the {{}}.

So it should be:

<app-page-header [heading]="myfield"></app-page-header>

and

<app-page-header [heading]="'MY_TEXT_FIELD' | translate:lang"></app-page-header>

You can look that up in the 'Template syntax' section of the Angular.io Cheatsheet.

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

1 Comment

Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.