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 class="item-note" item-right>
</p>
</button>
</ion-list>
<button (click)="getCategories()" ion-button color="primary">11</button>
</ion-content>
為什麼模版裡沒有顯示出數據,log出來的是undefinded,介面是有取得數據的
很明顯你的
categories
在log的时候还没有获取到数据,你使用的是异步获取数据,在还没有获取到数据的时候已经执行下面的console.log()
了。把log
放在then裡再看看。