1

I can retrieve the value in the view but not in the construtor. My component is in an ngFor and I want to edit each item independently. That's why I create a child component

 <li style="list-style-type: none;" *ngFor="let event of item.Events">
 ... <child-component [demo]="event"></child-component>

In the child component : I can display the result of demo in the view but I can not retrieve it in the constructor to be able to manipulate it

@Component({
    selector: 'child-component',
    queries: {
        content: new ViewChild('content')
    },
    templateUrl: 'build/pages/dashboard/timeline/timeline-feeling/index.html',
    ...
    inputs: ['demo'],
    ...
})

export class TimelinFeeling{
    static get parameters () {
        return [];
    }

    constructor () {
        this.demo = demo;
        console.log(this.demo); //return undefined
    }

2 Answers 2

1

Declare the property (TypeScript requirement) and decorate it with @Input (Angular requirement):

import { Input, ViewChild, ElementRef } from '@angular/core';
@Component({
    selector: 'child-component',
    templateUrl: 'build/pages/dashboard/timeline/timeline-feeling/index.html',
    ...
})

export class TimelinFeeling{
    @Input() public demo = null;
    @ViewChild('content') content: ElementRef;
    static get parameters () {
        return [];
    }

    ngOnInit () {
        this.demo = demo;
        console.log(this.demo); //return undefined
    }

Finally, check the value in ngOnInit instead.

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

2 Comments

thank you for your quick reply. I have this error with your solution : Syntax Error Unexpected token (31:18)
Okay, but did you use it as is? What line corresponds with (31:18) in your code?
0

Input values are not yet set in the constructor. Use ngOnInit() instead:

ngOnInit () {
    console.log(this.demo); //return undefined
}

3 Comments

thank you for your quick reply. I have this error with your solution : ORIGINAL EXCEPTION: ReferenceError: demo is not defined
You should have the same problem in the code in your question. Not sure what the intention was for this line. I guess you can just remove it.
Glad to hear :)

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.