angular.js - angular2 最基礎(chǔ)的問(wèn)題
問(wèn)題描述
import { Component ,OnInit} from ’@angular/core’;import { NavController, NavParams } from ’ionic-angular’;import { Category } from ’../../service/category’;import { CategoryService } from ’../../service/category.service’;@Component({ selector: ’index-page’, templateUrl: ’index.html’})export class IndexPage implements OnInit{ categories: Category[]; constructor( public navCtrl: NavController, public navParams: NavParams, private categoryService: CategoryService ) { } ngOnInit(): void { this.getCategories(); } getCategories(): void { this.categoryService.getCategories().then(categories => this.categories = categories); console.log(this.categories); } itemTapped(event, item) { console.log(window.localStorage.getItem(’token’)) // this.navCtrl.push(ListPage, { // id: item.title // }); // this.navCtrl.setRoot(ListPage); // console.log(localStorage.token); }}
index.html
<ion-header> <ion-navbar> <ion-title>車(chē)型選擇</ion-title> <button ion-button menuToggle end> <ion-icon name='menu'></ion-icon> </button> </ion-navbar></ion-header><ion-content> <ion-list> <button ion-item *ngFor='let category of categories' (click)='itemTapped($event, category)'> {{category.name}} <p item-right> </p> </button> </ion-list> <button (click)='getCategories()' ion-button color='primary'>11</button></ion-content>
為什么模版里沒(méi)有顯示出數(shù)據(jù),log出來(lái)的是undefinded,接口是有獲得數(shù)據(jù)的

問(wèn)題解答
回答1:很明顯你的categories在log的時(shí)候還沒(méi)有獲取到數(shù)據(jù),你使用的是異步獲取數(shù)據(jù),在還沒(méi)有獲取到數(shù)據(jù)的時(shí)候已經(jīng)執(zhí)行下面的console.log()了。把log放在then里再看看。
相關(guān)文章:
1. golang - 用IDE看docker源碼時(shí)的小問(wèn)題2. docker-machine添加一個(gè)已有的docker主機(jī)問(wèn)題3. docker images顯示的鏡像過(guò)多,狗眼被亮瞎了,怎么辦?4. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?5. docker - 如何修改運(yùn)行中容器的配置6. phpstudy8.1沒(méi)集成mysql-front7. 我何時(shí)應(yīng)該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)8. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!9. 前端 - @media query 使用出現(xiàn)的問(wèn)題?10. html5和Flash對(duì)抗是什么情況?

網(wǎng)公網(wǎng)安備