ionic3+Angular4实现接口请求及本地json文件读取示例

 更新时间:2017年10月11日 17:16:28   作者:Hank_谢旱  
本篇文章主要介绍了ionic3+Angular4实现接口请求及本地json文件读取示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一 准备工作

首先,ionic3+Angular4的开发环境你得有,这里就不赘述。环境准备好,创建一个空白项目,模板自选。

二 实现过程

1 新建json文件和service

service记得在app.module.ts中引用

json和service

2 json文件格式

格式类似这样,根据实际需求决定。

[
 {
  "id":"1",
  "name":"xiehan",
  "age":"24",
  "message":"测试json文件读取"
 },
 {
  "id":"2",
  "name":"xiehan",
  "age":"24",
  "message":"测试json文件读取"
 },
 {
  "id":"3",
  "name":"xiehan",
  "age":"24",
  "message":"测试json文件读取"
 },
 {
  "id":"4",
  "name":"xiehan",
  "age":"24",
  "message":"测试json文件读取"
 }
]

3 service

import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Http, Response} from '@angular/http';
import "rxjs/add/operator/map";


@Injectable()
export class DemoService {

 constructor(private httpService: Http){
 }
 // 网络接口请求
 getHomeInfo(): Observable<Response> {
  return this.httpService.request('http://jsonplaceholder.typicode.com/users')
 }

 // 本地json文件请求
 getRequestContact(){
  return this.httpService.get("assets/json/message.json")
 }
}

4 数据显示

1 网络接口请求

//home.ts
import {ChangeDetectorRef, Component} from '@angular/core';
import { NavController } from 'ionic-angular';
import {DemoService} from "../../services/demo.service";

@Component({
 selector: 'page-home',
 templateUrl: 'home.html'
})
export class HomePage {
 // 接收数据用
 listData: Object;
 // 依赖注入
 constructor(public navCtrl: NavController,
       private ref: ChangeDetectorRef,
       private demoService: DemoService,) {
 }

 ionViewDidLoad() {
  // 网络请求
  this.getHomeInfo();
 }

 getHomeInfo(){
  this.demoService.getHomeInfo()
   .subscribe(res => {
    this.listData = res.json();
    // 数据格式请看log
    console.log("listData------->",this.listData);
    this.ref.detectChanges();
   }, error => {
    console.log(error);
   });
 }
}

 
//home.html
<ion-header>
 <ion-navbar>
  <ion-title>首页</ion-title>
 </ion-navbar>
</ion-header>

<ion-content padding>
 <ion-list *ngFor="let item of listData">
  <ion-item>
  <!--?是Angular特定语法,相当于判断数据是否存在,有则显示无则不显示-->
   {{item?.name}}
  </ion-item>
 </ion-list>
</ion-content>

效果图


2 本地json文件请求

service中已经写了getRequestContact()方法对本地json文件读取。

//contact.ts
import {ChangeDetectorRef, Component} from '@angular/core';
import { NavController } from 'ionic-angular';
import {DemoService} from "../../services/demo.service";

@Component({
 selector: 'page-contact',
 templateUrl: 'contact.html'
})
export class ContactPage {

 contactInfo=[];

 constructor(public navCtrl: NavController,
       private demoService: DemoService,
       private ref: ChangeDetectorRef,) {

 }

 ionViewDidLoad() {
  // 网络请求
  this.getRequestContact();
 }

 getRequestContact(){
  this.demoService.getRequestContact()
   .subscribe(res => {
    this.contactInfo = res.json();
    console.log("contactInfo------->",this.contactInfo);
    this.ref.detectChanges();
   }, error => {
    console.log(error);
   });
 }
}

// contact.html
<ion-header>
 <ion-navbar>
  <ion-title>
   联系人
  </ion-title>
 </ion-navbar>
</ion-header>

<ion-content>
 <ion-list>
  <ion-item *ngFor="let item of contactInfo">
   <div style="display: flex;flex-direction: column;">
    <span>姓名:{{item?.name}}</span>
    <span>年龄:{{item?.age}}</span>
    <span>信息:{{item?.message}}</span>
   </div>
  </ion-item>
 </ion-list>
</ion-content>

效果图


三 总结

1.所有创建的page要在app.module.ts中引用;
2.service要在app.module.ts中引用;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • AngularJs Injecting Services Into Controllers详解

    AngularJs Injecting Services Into Controllers详解

    本文主要介绍AngularJs Injecting Services Into Controllers的知识,这里整理了一下相关资料,及示例代码,帮助大家学习和理解,有兴趣的小伙伴可以参考下
    2016-09-09
  • 详解Angular依赖注入

    详解Angular依赖注入

    依赖注入(DI -- Dependency Injection)是一种重要的应用设计模式。Angular里面也有自己的DI框架,在设计应用时经常会用到它,它可以我们的开发效率和模块化程度。&#160;Angular系统中通过在类上添加@Injectable装饰器来告诉系统这个类(服务)是可注入的。
    2021-05-05
  • AngularJS 2.0入门权威指南

    AngularJS 2.0入门权威指南

    这篇文章主要介绍了AngularJS 2.0入门权威指南的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • AngularJS中transclude用法详解

    AngularJS中transclude用法详解

    这篇文章主要介绍了AngularJS中transclude用法,详细分析了transclude的具体功能、使用技巧与相关注意事项,需要的朋友可以参考下
    2016-11-11
  • Angular整合zTree的示例代码

    Angular整合zTree的示例代码

    本篇文章主要介绍了Angular整合zTree的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • 详解angularJS自定义指令间的相互交互

    详解angularJS自定义指令间的相互交互

    本篇文章主要介绍了详解angularJS自定义指令间的相互交互,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Angular.JS实现无限级的联动菜单(使用demo)

    Angular.JS实现无限级的联动菜单(使用demo)

    这篇文章主要介绍了Angular.JS中实现无限级联动菜单的使用示例,本文是在之前的一篇文章的基础上进行的几个demo分享,有需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • Angular使用ControlValueAccessor创建自定义表单控件

    Angular使用ControlValueAccessor创建自定义表单控件

    这篇文章主要介绍了Angular使用ControlValueAccessor创建自定义表单控件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Angular HMR(热模块替换)功能实现方法

    Angular HMR(热模块替换)功能实现方法

    本篇文章主要介绍了Angular HMR(热模块替换)功能实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • angularjs中$http异步上传Excel文件方法

    angularjs中$http异步上传Excel文件方法

    本篇文章给大家详细分析了angularjs中$http异步上传Excel文件方法,对此有需要的读者可以学习下。
    2018-02-02

最新评论