0

Consider the below code, or see this plunk: https://plnkr.co/edit/mwPHfz?p=info.

Master

import {Component} from "angular2/core";
import {DetailComponent} from "./detail.component";
import {User} from "./user";
import {MOCKUSERS} from "./mock-users";

@Component({
    directives: [DetailComponent],
    selector: "master",
    template: `
        <h2>Master</h2>

        <div>
            <ul>
                <li *ngFor="#user of users" (click)="selectUser(user)">
                    {{user.firstName}} {{user.lastName}}
                </li>
            </ul>
        </div>

        <div>
            <detail [user]="selectedUser"></detail>
        </div>
    `
})
export class MasterComponent {
    users: User[] = MOCKUSERS;
    selectedUser: User;

    selectUser(user: User) {
        this.selectedUser = user;
    }
}

Detail

import {Component, Input} from "angular2/core";
import {User} from "./user";

@Component({
    selector: "detail",
    template: `
        <h2>Detail</h2>
        <div>
            <div>
                <input type="text" [(ngModel)]="user.firstName">
            </div>
            <div>
                <input type="text" [(ngModel)]="user.lastName">
            </div>
            <div>
                <button type="submit">Submit</button>
            </div>
        </div>
    `
})
export class DetailComponent {
    @Input()
    user: User;
}

User

export class User {
    firstName: string,
    lastName: string
}

When you click on a name in the list (master) the form (detail) should be updated. The first name and last name properties should appear in the firstName and lastName inputs respectively.

However, when you look in the developer tools, you can see the following error when loading the app:

EXCEPTION: TypeError: l_user0 is undefined in [user.firstName in DetailComponent@4:27]

So somehow the binding to user doesn't seem to be accepted. Can anyone tell why?

3 Answers 3

3

You are passing an undefined object (before selecting a user) to DetailComponent. You should only render the DetailComponent if there is a selectedUser.

<div *ngIf="selectedUser">
  <detail [user]="selectedUser"></detail>
</div>

Working plunker. In index.html, I had to replace the imported angular js file from angular.min.js to angular.dev.js to fix an unrelated issue on your plunker.

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

2 Comments

I had already tried the *ngIf but the main issue for me was indeed the angular file that needed to change. In another project I switched from beta 6 to beta 8 and things started working there as well.
@Aetherix there are still some open issues on the minified bundle, check #6380
0
<ul class="list-group users-list">
    <li class="list-group-item"
        *ngFor="let user of users"
        (click)="selectUser(user)"
        [class.active]="user === activeUser">
        {{ user.name }} ({{ user.username }})
    </li>
</ul>

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

you should initilize selectedUser parametes.

export class MasterComponent {
    users: User[] = MOCKUSERS;
    selectedUser: User = null;

    selectUser(user: User) {
        this.selectedUser = user;
    }
}


<div>
  <detail [user]="selectedUser"></detail>
</div>



@Component({
    selector: "detail",
    template: `
        <h2>Detail</h2>
        <div *ngIf="user !== null">
            <div>
                <input type="text" [(ngModel)]="user.firstName">
            </div>
            <div>
                <input type="text" [(ngModel)]="user.lastName">
            </div>
            <div>
                <button type="submit">Submit</button>
            </div>
        </div>
    `
})

export class DetailComponent {
    @Input() user: User = null;
}

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.