angular.js - angular2 最基础的问题
某草草
某草草 2017-05-15 17:09:40
[AngularJS讨论组]
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


  
    车型选择
    
  



  
    
  
  

为什么模版里没有显示出数据,log出来的是undefinded,接口是有获得数据的

某草草
某草草

全部回复(1)
ringa_lee

很明显你的categories在log的时候还没有获取到数据,你使用的是异步获取数据,在还没有获取到数据的时候已经执行下面的console.log()了。把log放在then里再看看。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号