1

I think I am doing everything right here.

Although the browser console shows the error "Cannot read property 'name' of undefined":

enter image description here

My media.component.ts looks like following:

import { Component,Input } from '@angular/core';
@Component({
selector: 'my-media-component',
templateUrl: `./app/media.component.html`,
styleUrls: [`./app/media.component.css`]
})
export class MediaAppComponent{
@Input() mediaItem;

onDelete(){
 console.log('deleting....');
}
}

My app.component.ts looks like:

import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: `./app/app.component.html`,
 })
export class AppComponent  {
 firstMediaItem={
 name: 'sahil'
};
}

app.component.html looks like:

its, working
 <my-media-component [mediaItem]="firstMediaItem"></my-media-component>

media.component.html looks like:

<h1>{{mediaItem.name}}</h1>

4 Answers 4

6

Use the safe-navigation operator to guard agains mediaItem not yet being set when Angular tries to resolve the binding:

<h1>{{mediaItem?.name}}</h1>

Update

In your Plunker you have MediaAppComponent listed in @NgModule({ bootstrap: [App, MediaAppComponent]}) but this component shouldn't be there if it is used as a normal component. Only root components should be listed there.

Plunker example

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

10 Comments

It removes the errors from the console , yet purpose to display the name is not resolved...how can i view my name on browser
If you pass the value to @Input() mediaItem like [mediaItem]="'firstMediaItem'", then you pass a string, that doesn't have a name` property. Therefore it's expected that you won't get anything shown.
I changed it to : [mediaItem]="firstMediaItem" , still its unable to get mediaItem as an object .. My browser Console shows : <my-media-component _nghost-vax-1="" ng-reflect-media-item="[object Object]" ng-version="2.4.6"> <h1 _ngcontent-vax-1=""></h1> </my-media-component> ............ The <h1> tag remains empty ?!!!!
Agree with Günter Zöchbauer, the problem definately lies not in the piece of code you mentioned.
Yes i think , I did everything right here...M really stuck in what's the problem ????????
|
2

Remove quotes "'firstMediaItem'" just <my-media-component [mediaItem]="firstMediaItem"></my-media-component>

1 Comment

Thanks for the suggestion , I did but nothing happened...now when i view the source in browser it shows : <my-media-component _nghost-cjq-1="" ng-reflect-media-item="[object Object]" ng-version="2.4.6"> <h1 _ngcontent-cjq-1=""></h1> </my-media-component>
1

use elvis operator ? instead

<h1>{{mediaItem?.name}}</h1>

this will prevent angualr to thorw any error if data is not present, and allow data to display asynchronously

Comments

0

Change it

[mediaItem]="'firstMediaItem'" ---> [mediaItem]="firstMediaItem"

After use like

<h1 *ngIf="mediaItem">{{mediaItem.name}}</h1>

1 Comment

Already Did that....Still error remains the same.... Refer the comments on Günter Zöchbauer's ANSWER

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.