I am trying to create a separate component to deal with the presentation of a news feed. It works fine when I have everything within app.component.ts, but when I separate the code into its own component (news-feed.component.ts) it initially displays fine but when a menu button is clicked (e.g.: <button (click)="getNews('the-telegraph')">telegraph</button>) it is failing to recognise the function 'getNews' and the following error is generated:'EXCEPTION: Error in ./AppComponent class AppComponent - inline template:4:12 caused by: self.context.getNews is not a function'.
Please note - it is successfully running the getNews() function within the NewsFeedComponent when the site first loads.
Here is app.component.ts:
import {Component} from '@angular/core';
import {NewsService} from './news.service';
import {Observable} from 'rxjs/Rx';
import { NewsFeedComponent } from './news-feed.component';
@Component({
selector: 'news-app',
template:`
<h1>News Feed</h1>
<nav>
<button (click)="getNews('bbc-news')">bbc</button>
<button (click)="getNews('the-telegraph')">telegraph</button>
<button (click)="getNews('the-guardian-uk')">guardian</button>
<button (click)="getNews('independent')">independent</button>
<button (click)="getNews('financial-times')">financial times</button>
<button (click)="getNews('the-economist')">the economist</button>
<button (click)="getNews('sky-news')">sky news</button>
<button (click)="getNews('the-new-york-times')">the new york times</button>
</nav>
<news-feed></news-feed>
`,
styleUrls: ['app/app.component.css']
})
export class AppComponent {
}
Here is news-feed.component.ts:
import {Component} from '@angular/core';
import {NewsService} from './news.service';
import {Observable} from 'rxjs/Rx'
@Component({
selector: 'news-feed',
template: `
<ul>
<li *ngFor="let article of news">
hello2
<a href="{{article.url}}" target="_bank"><h2>{{article.title}}</h2></a>
<p>{{article.publishedAt}}</p>
<img src="{{article.urlToImage}}" style="width: 200px;">
<p>{{article.description}}</p>
</li>
</ul>
`
})
export class NewsFeedComponent {
// public news;
constructor(private _newsService: NewsService) { }
ngOnInit() {
this.getNews('bbc-news');
}
getNews(source) {
this._newsService.getNews(source).subscribe(
// the first argument is a function which runs on success
data => {
this.news = data['articles']
},
error => {
//console.error("Error saving food!");
return Observable.throw(error);
}
);
}
}
Here is app.module.ts:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import {FormsModule} from '@angular/forms';
import {NewsService} from './news.service'
import { AppComponent } from './app.component';
import { NewsFeedComponent } from './news-feed.component';
@NgModule({
imports: [BrowserModule,HttpModule,FormsModule],
declarations: [
AppComponent,
NewsFeedComponent
],
providers: [NewsService],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
bootstrap: [AppComponent]
})
export class AppModule { }
Apologies for cutting and pasting in all this code. I have tried to get this working on Plunker and have failed due to being very stupid. Any help would be much appreciated.
getNewsmethod inside app components template but your method is inside your child component.getNewsis expected on yourAppComponentclass, because it is outside yournews-feed