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>車型選擇</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. python 利用subprocess庫(kù)調(diào)用mplayer時(shí)發(fā)生錯(cuò)誤2. python文檔怎么查看?3. python - Pycharm的Debug用不了4. javascript - 關(guān)于apply()與call()的問(wèn)題5. datetime - Python如何獲取當(dāng)前時(shí)間6. javascript - nginx反向代理靜態(tài)資源403錯(cuò)誤?7. html - eclipse 標(biāo)簽錯(cuò)誤8. 請(qǐng)問(wèn)PHPstudy中的數(shù)據(jù)庫(kù)如何創(chuàng)建索引9. 安全性測(cè)試 - nodejs中如何防m(xù)ySQL注入10. python - pycharm 自動(dòng)刪除行尾空格
