如何在单独的页面上显示卡的内容[角度]

我是Angular的新手。我可能无法正确提出问题。但是请让我解释一下。我已经创建了一个MEAN应用程序。它部署在heroku上。这是link。如果单击任何卡,即Read more按钮,您将自动滚动到显示内容的div。但是,我希望将此内容显示在单独的全新页面上,因为我正计划关联其他功能,例如LikeUpvoteReport等。请告诉我一些方向。

PS:Here是github仓库。我一直向所有人开放。

chensiduan 回答:如何在单独的页面上显示卡的内容[角度]

我不知道确切缺少哪一部分,但是:

  • 如果您需要导航到其他页面: link

  • 如果您不知道如何将数据带到其他页面:link

,

我假设您有ngFor可以用这样的角度articles-list.component.html显示文章

<div class="article" *ngFor="let card of cards" (click)="goToArticle(card )">
  // your card html here
</div>

现在您有2个选择。

选项1

一种方法是在服务变量中的单击集中设置一个service对象,然后在article object这样的另一页上获取该data.service.ts

clickedArticle : Object = {}; 

setCLickedArticleObj(obj){
   clickedArticle  = obj;
}

getClickedArticle(){
   return clickedArticle;  
}

在您的ariticles-list.component.ts

实现这样的功能

goToArticle(article){
  this._dataService.setCLickedArticleObj(article)
  this.router.navigate('/article-details')
}

在新的page/component上,您可以像这样获得点击的对象

// service import 
import {DataService} from 'your-path-to-service'
constructor(private _dataService : DataService){
}
incomingArticleObject : Object = {};
ngOnInit(){
   this.incomingArticleObject  = this._dataService.getClickedArticle()
}

选项2

在选项2中,我假设您正在接收_id API的每篇文章back-end,因此您可以将点击的文章的_id设置为localstorage,并在新的page/component可以通过点击article detail api获得有关新组件加载的单击的文章详细信息。 对于此解决方案,您可以像这样在服务中实现功能

setClickedArticle(id){
   localStorage.setItem('__ai',id)
}

getClickedAritcleId(){
   return localStorage.getItem('__ai');  
}

然后在您的articles-list.component.html中,您将像这样

 <div class="article" *ngFor="let card of cards">
  // your card html here
</div>

在您的articles-list.component.ts中,您将拥有这样的功能

articledId : String = '';

goToArticleDetails(id){
   this._dataService.setClickedArticle(id);
   this.router.navigate('/article-details');
}

在新的page/component article-details.compnent.ts上,您可以像这样在ngOnInit上获得该文章的详细信息

articleId : String = '';
articleDetails : Object = {};
ngOnInit(){
   this.articleId  = this._dataService.getClickedAritcleId()
   this._dataService.getArticleById(this.articleId).subscribe(article=>{
         this.articleDetails  = article;
   })
}
,

Here is similiar working demo

您可以使用路由器执行相同的操作。

 RouterModule.forRoot([
  {path:'',redirectTo: 'list',pathMatch:'full'},{path:'list',component:ListComponent},{path: 'home/:id',component:HomeComponent}
  ])
],
本文链接:https://www.f2er.com/3110517.html

大家都在问