在我单击 link 两次之前,我的新闻提要不会显示
my news feed don't display until i click the link twice
我正在开发这个显示来自 Firebase 数据库的新闻的小应用程序,但是当我突然转到新闻 link 或提要时,它不会显示,直到我在导航栏中单击 link 两次我我正在使用 angular 请帮忙
这是我的服务文件:
import {Injectable} from '@angular/core';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable({
providedIn: 'root'
})
export class NewsService {
list: any[];
constructor(db: AngularFireDatabase) {
db.list('/actu').valueChanges()
.subscribe(list => {
this.list = list;
console.log(this.list);
});
}
Getlist() {
return this.list;
}
getnews(title: string) {
return this.list.find(value => value.titre === title);
}
}
构造函数中的调用最好不要不执行,服务内部的observable也不要subscribe()。相反,return 它并在显示此数据的组件中订阅它。
news.service.ts
import {Injectable} from '@angular/core';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable({
providedIn: 'root'
})
export class NewsService {
constructor(private db: AngularFireDatabase) {
}
Getlist() {
return db.list('/actu').valueChanges();
}
}
news.component.ts
import { NewsService } from './news.service';
//component declarations and imports ...
export class NewsComponent implements OnInit {
list: any[];
constructor(private newsService: NewsService){}
buttonClicked(){
this.newsService.Getlist().subscribe(list => {
this.list = list;
console.log(this.list);
})
}
}
news.component.html
<a (click)="buttonClicked()">nav button</a>
当然,这不会是您的服务、组件或 html 中的唯一代码。这只是突出显示您的问题的代码。
我正在开发这个显示来自 Firebase 数据库的新闻的小应用程序,但是当我突然转到新闻 link 或提要时,它不会显示,直到我在导航栏中单击 link 两次我我正在使用 angular 请帮忙
这是我的服务文件:
import {Injectable} from '@angular/core';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable({
providedIn: 'root'
})
export class NewsService {
list: any[];
constructor(db: AngularFireDatabase) {
db.list('/actu').valueChanges()
.subscribe(list => {
this.list = list;
console.log(this.list);
});
}
Getlist() {
return this.list;
}
getnews(title: string) {
return this.list.find(value => value.titre === title);
}
}
构造函数中的调用最好不要不执行,服务内部的observable也不要subscribe()。相反,return 它并在显示此数据的组件中订阅它。
news.service.ts
import {Injectable} from '@angular/core';
import {AngularFireDatabase} from 'angularfire2/database';
@Injectable({
providedIn: 'root'
})
export class NewsService {
constructor(private db: AngularFireDatabase) {
}
Getlist() {
return db.list('/actu').valueChanges();
}
}
news.component.ts
import { NewsService } from './news.service';
//component declarations and imports ...
export class NewsComponent implements OnInit {
list: any[];
constructor(private newsService: NewsService){}
buttonClicked(){
this.newsService.Getlist().subscribe(list => {
this.list = list;
console.log(this.list);
})
}
}
news.component.html
<a (click)="buttonClicked()">nav button</a>
当然,这不会是您的服务、组件或 html 中的唯一代码。这只是突出显示您的问题的代码。