Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: Input variable in Component does not update once called

Tags:

Hello people of StackOverflow!

I am having some trouble with my code. As you can see, I want to be able to call a row with a set width (bootstrap format), as I don't want to type the class every time.

So I thought of a way which is the following:

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

@Component({
    moduleId: module.id,
    selector: 'content',
    template: ` <div class="row">
                    <div [ngClass]="contentClass" 
                         id="content" 
                         [ngStyle]="{ 'color': 'black', 'font-size': '20px' }">
                    <ng-content></ng-content>
                    </div>
                </div>`,
    styleUrls: ['content.stylesheet.css']
})

export class ContentComponent {
    @Input() rowWidth = "12";
    contentClass=(("col-lg-" + this.rowWidth)+(" col-sm-" + this.rowWidth)+(" col-xs-" + this.rowWidth));
}

But once I call the component from another component, it's not working the way I want.

<banner bannerHeight="300px"></banner>   <!-- This works -->
<content rowWidth="6"></content>         <!-- This doesn't -->

If I used for example

<content [ngStyle]="{'color': 'black'}"></content>

the operation succeeds. The directives and imports are set correctly in the parent component.

So here is my question: How do I make it work the way I want it to?

like image 719
yollator Avatar asked Jun 24 '16 19:06

yollator


People also ask

Why Angular input setter is only being fired once?

If you click on the “set to Parent” button multiple times, the child component setter won't be triggered from 2nd time onward. Thus it looks like the Input setter is only being fired once if the value of the setter hasn't been changed.

How Angular 2 detect changes?

How is change detection implemented? Angular can detect when component data changes, and then automatically re-render the view to reflect that change.

How does Input() work Angular?

Input decorator marks the property as the input property. I.e it can receive data from the parent component. The parent component uses the property binding to bind it to a component property. Whenever the value in the parent component changes angular updates the value in the child component.


1 Answers

It doesn't work (the way you want, i assume you mean that your contentClass has a rowWidth of 12) because your assignment to contentClass is made before the template is actually initialized.

You have to implement OnInit and use ngOnInit to set the contentClass with the assignment to your rowWidth input:

export class ContentComponent implements OnInit{
    @Input() rowWidth = 12;
    contentClass:string;

    ngOnInit():any {
        this.contentClass = (("col-lg-" + this.rowWidth)+(" col-sm-" + this.rowWidth)+(" col-xs-" + this.rowWidth));
    }
}

With <content [rowWidth]="6"></content> your element will then have col-lg-6 col-sm-6 col-xs-6 instead of 12 set as its css classes.

like image 106
malifa Avatar answered Sep 28 '22 02:09

malifa