
[英]Angular execute a method on Component1 triggered from component2 (unrelated comps)
[英]not getting updated data from service to my component1 onclick event in component2 in angular
我正在使用 angular 9 来制作新闻应用程序。我正在使用 Times of India RSS Feed API https://timesofindia.indiatimes.com
这是我的 stackblitz 链接https://stackblitz.com/github/mehulk05/Newsapp
我的主页组件中有类别,因此当我单击任何类别时,我会在我的服务中获取该类别的新闻并尝试在 NewsList 组件中显示它。但不知何故,当我更改类别时,我无法发送最新数据从服务到 newsList 组件。虽然我在 NewsService 的控制台中获取更新的数据,但无法在 newsListComponent 中获取该数据
这是我的代码
主页.component.html
<div class="container mt-5">
<div class="col-xs-12">
<div class="row mt-4">
<div class="col-xs-3 col-md-3 sidelink">
<div class="list-group">
<a *ngFor="let c of categories"
class="list-group-item list-group-item-action"
id="list-home-list" [routerLink]=""
role="tab" aria-controls="home"
( (click)='onClick(c.url)'>{{c.type}}</a>
</div>
</div>
<div class="col-md-9 col-xs-9">
<router-outlet></router-outlet>
</div>
</div>
</div>
</div>
主页.component.ts
export class HomeComponent implements OnInit {
categories=[
{type:"home",url:"https://timesofindia.indiatimes.com/rssfeedstopstories.cms?feedtype=sjson"},
{type:"recent",url:"https://timesofindia.indiatimes.com/rssfeeds/1221656.cms?feedtype=sjson"},
{type:"india",url:"https://timesofindia.indiatimes.com/rssfeeds/-2128936835.cms?feedtype=sjson"},
{type:"cricket",url:"https://timesofindia.indiatimes.com/rssfeeds/4719161.cms?feedtype=sjson"}]
constructor(private ns:NewsserviceService,
private router:Router) { }
ngOnInit(): void {
}
onClick(title){
console.log(title)
this.ns.getUrl(title)
this.router.navigate(["/news-list"])
}
}
新闻服务.ts
export class NewsserviceService {
url:string='https://timesofindia.indiatimes.com/rssfeedstopstories.cms?feedtype=sjson'
constructor(private http:HttpClient) {
}
getUrl(url){
this.url=url
this.getNewsList();
}
getNewsList(){
this.url='https://cors-anywhere.herokuapp.com/'+this.url
console.log(this.url)
this.http.get(this.url ).subscribe(data=>{
console.log(data)
})
}
}
新闻列表.component.ts
news:any
constructor(private ns:NewsserviceService,private cd:ChangeDetectorRef) { }
ngOnInit() {
this.news= this.ns.getNewsList();
console.log("news is" +this.news)
}
}
当我单击任何类别时,我将第一次在控制台中从 Newslist 组件获取数据,但是之后我没有从 newsList 组件中获取任何数据,但我从 Newservice 中获取了控制台中的数据
在这里我可以看到你想要做的是
现在你可以做的是你可以在服务中创建一个事件发射器,并将该事件从组件 1 和服务数据发送到组件 2。那么如何做到这一点,它是
服务.ts
SelectedUrlData=new EventEmitter();
getUrl(url){
this.url=url
this.getNewsList();
}
getNewsList() {
this.url = 'https://cors-anywhere.herokuapp.com/' + this.url
this.http.get<any[]>(this.url).subscribe(data => {
this.tohome = data
})
}
getData(): Observable<any> {
this.url='https://cors-anywhere.herokuapp.com/'+this.url
return this.http.get<any[]>(this.url);
}
主页.ts
@Input() newdata:any
ngOnInit(): void {
this.ns.getData().subscribe(data=>
{
console.log(data)
})
}
onClick(title){
this.ns.getUrl(title)
this.tohome=this.ns.tohome
this.ns.selectedurl.emit(this.tohome)
this.router.navigate(["/news-list"])
}
新闻列表.ts
news:any
ngOnInit() {
this.ns.getData().subscribe(data=>
{
console.log(data)
})
this.news = this.ns.getNewsList();
this.ns.selectedurl.subscribe(data => {
console.log(data)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.