2

I am having trouble getting angular2 to pass input into a child component. Within the child component, the object is always undefined. Here is my code.

My markup in the parent component

<peoplesearchlist [people]="peopleSearchData"></peoplesearchlist>

The child component

import {Component, View, Input} from 'angular2/angular2';

@Component({
    selector: 'peoplesearchlist',
    inputs: ['people']
})
@View({
    templateUrl: './directory/peopleSearchList.html'
})
export class PeopleSearchList {
    constructor() {
    console.log('People-Search-List:' + this.people);
    }
}

Abbreviated Parent Component

import {Component, View, Input, bootstrap} from 'angular2/angular2';
import {Http, Headers} from 'angular2/http';
import {PeopleSearchBar} from './peopleSearchBar';
import {PeopleSearchList} from './peopleSearchList';

@Component({
    selector: "directory-people-search"
})
@View({
    templateUrl: "./directory/peopleSearch.html",
    directives: [PeopleSearchBar, PeopleSearchList]
})
export class PeopleSearch
{
    constructor(http: Http) {
         this.searchString = '';
         this.http = http;

        this.peopleSearchData = {
            faculty: [],
            students: [],
            retirees: []
        };

        console.log('People-Search' + this.peopleSearchData);
    }   
}

If I look in my console, I see first the log from the parent component with the object, then the log from the child component as undefined. I've tried using @Input people, but have the same behavior.

I am using ES6 with traceur. I have looked at this issue and could not resolve my issue: Angular 2 Component @Input not working

1
  • Print it inside afterViewInit, at construction time it won't work. Commented Nov 25, 2015 at 15:45

3 Answers 3

2

I know this question is already 9 months old and you've probably managed to do it yourself by still:

The problem is that you're trying to get component's value in the constructor which is called when this component is instantiated but the bindings aren't initialized yet. You need to implement AfterViewInit interface instead of the constructor:

@Component(...)
export class PeopleSearchList implements AfterViewInit {
    constructor() {
    }

    ngAfterViewInit() {
        console.log('People-Search-List:' + this.people);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like, it worked for me:

import {Component, View, Input} from 'angular2/angular2';

@Component({
    selector: 'peoplesearchlist'
})
@View({
    templateUrl: './directory/peopleSearchList.html'
})
export class PeopleSearchList {
    @Input() people: Object
    constructor() {
    console.log('People-Search-List:' + this.people);
    }
}

Comments

0

Use ngOnChanges() to detect when data comes in your input. The constructor is called before the input is available.

ngOnChanges(): void {
    console.log('People-Search-List:' + this.people);
}

Comments

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.