【问题标题】:Ionic 2 (angular 2 typescript) - Cannot applying data to viewIonic 2 (angular 2 typescript) - 无法应用数据来查看
【发布时间】:2018-01-14 21:45:02
【问题描述】:

我被卡住了,因为 ionic 2 。 我有一个问题,我想使用带有文章标题和缩略图等其他数据的*ngFor 呈现卡片组件。 当我在数组中硬编码值时,卡片显示。但是,当我使用 REST API 卡时不会呈现。数据检索成功。

在角度 1 中是否有任何类似 $scope.$digest(); 用于在 $scope 上为角度 2(离子 2)应用更改 或者如何得到我想要的?

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import * as $ from 'jquery';
import * as moment from 'moment';
var itemList =[];
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})



export class HomePage  {
  

  constructor(public navCtrl: NavController) {
    moment.locale('id');
    this.getNews();
  }


  getNews(){
    $.ajax({
      url: secret_api_url ,
      dataType:"jsonp",
      method:"get",
      success:(data =>{
        var post = data.posts;
        post.forEach(function(p){
          p.date = moment(p.date).format('lll');
          itemList.push(p);
        });
        // itemList = post;

      }),
      error:(err =>{
        console.log("Error",err);
      })
    });
  }










}
    <ion-card padding *ngFor="let item of itemList;">

    <ion-card-content>
      <label style="color:white;">
        {{i}}
        <label class="timestamp">{{ item.date }}</label>
      </label><br>
      <small>
            <i>
            {{ item.short_desc }}
            </i>
      </small>
      <ion-card-title>
        {{item.title}}
      </ion-card-title>
    </ion-card-content>
    <img src="{{item.img_url}}"/>
  </ion-card>

【问题讨论】:

  • 贴一些代码
  • 请给你的问题一个描述它的标题。
  • 完成,发布了一些代码并描述了标题。对不起我的不好,因为我太卡在那里了

标签: angular typescript ionic-framework ionic2


【解决方案1】:

确保来自您的 REST API 的数据正确转换为类的实例:

export class Article{
    constructor(public id: string, public title: string) {}
}

// Your component:

this.articles: Array<Articles> = [];

ngOnInit() {
    this.articleService.getArticles()
        .subscribe(articles => { 
                this.articles = articles.map(item => new Article(item.id, item.title);
            }); 
        });      
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多