根据Angular2中的Dataabse值动态加载数据卡

时间:2016-05-20 03:42:26

标签: javascript mongodb angular material-design

我有这个来自材料设计的演示卡,如何根据数据库值显示多个数据卡。

如果我的数据库中的值是4,那么我需要在UI中显示4个数据卡。 我的后端数据库是mongoDB。

<!-- Square card -->
<style>
.demo-card-square.mdl-card {
  width: 320px;
  height: 320px;
}
.demo-card-square > .mdl-card__title {
  color: #fff;
  background:
    url('../assets/demos/dog.png') bottom right 15% no-repeat #46B6AC;
}
</style>

<div class="demo-card-square mdl-card mdl-shadow--2dp">
  <div class="mdl-card__title mdl-card--expand">
    <h2 class="mdl-card__title-text">Update</h2>
  </div>
  <div class="mdl-card__supporting-text">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Aenan convallis.
  </div>
  <div class="mdl-card__actions mdl-card--border">
    <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
      View Updates
    </a>
  </div>
</div>    

1 个答案:

答案 0 :(得分:1)

如果你有这样的dataCard模型。

dataCard = {
 "title":"",
 "bodyText" : "",
 "actionText":""
};

//and your service returned an array of dataCard objects.

@Component({
   selector:'dataCard',
   template : `
<div *ngFor="#card of _dataList" class="demo-card-square mdl-card mdl-shadow--2dp">
   <div class="mdl-card__title mdl-card--expand">
     <h2 class="mdl-card__title-text">{{card.title}}</h2>
   </div>
  <div class="mdl-card__supporting-text">
    {{card.bodyText}}
  </div>
  <div class="mdl-card__actions mdl-card--border">
    <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
      {{card.actionText}}
    </a>
  </div>
</div>`
})
export class DataCard implements OnInit{
  private _dataList : Type;
  private other.....,

  constructor(service:Service){
  // setup
  }

  ngOnInit(){
   this.service.fetchDataForCards()
   .subscribe(data => this._dataList = data);
  }
}