1.创建服务

打开命令窗口,cd到项目目录下,输入  ng g service myData1  回车 创建服务,如下图所示:

这样就成功创建了服务,此时,可以在项目的app文件夹下生成了两个service文件,

2.引入注册服务

服务创建好之后,要先引入注册之后才能用。

首先要在app.module.ts里:

引入     import { MyDataService } from './my-data.service';

注册      providers:[MyDataService];

app.module.ts整体代码如下:
import { NgModule }      from '@angular/core';//引入angular核心模块
import { BrowserModule } from '@angular/platform-browser'; //浏览器解析
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here //引入组件
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { NewsComponent } from './components/news/news.component'; //1.引入服务 注册服务
import { MyDataService } from './my-data.service'; @NgModule({
imports: [ //配置模块 /*引入模块 请求数据模块*/
BrowserModule,
FormsModule // <-- import the FormsModule before binding with [(ngModel)]
],
declarations: [ //声明 注册 组件 所有自定义的组件都要在这里声明
AppComponent,
HeaderComponent,
NewsComponent
],
providers:[MyDataService], /*服务 工具*/
bootstrap: [ AppComponent ] //启动模块 /*加载根组件*/
})
export class AppModule { } //暴露跟模块
app.module.ts里引入注册之后,还需要在用到服务的地方引用,我写的demo是在news组件里用到了MyDataService服务,所以就在news.component.ts里引入
//要用服务 1.需要在app.module.ts 引入和注册    2.在使用的地方引入

import { MyDataService } from '../../my-data.service';

这样就可以在news.component.ts中使用MyDataService服务了;

3.使用服务

使用服务就是把服务实例化,在news.component.ts中用构造函数来实例化我们定义的服务:

  constructor(private  storage:MyDataService) {
console.log(this.storage);
this.news = this.storage.getItem('msgList') || [];
}

这样就可以使用服务了。

我这里写了一个小demo,使用服务实现数据的缓存处理:

html:

<h3>{{newsTitle}}</h3>
<input type="text" [(ngModel)]="currentMsg"><button (click)="addList()">增加+</button>
<ul>
<li *ngFor="let item of news;let key =index">
{{item}}------<button (click)="delete(key)">删除</button>
</li>
</ul>

news.component.ts:

import { Component, OnInit } from '@angular/core';

//要用服务 1.需要在app.module.ts 引入和注册    2.在使用的地方引入

import { MyDataService } from '../../my-data.service';

@Component({
selector: 'app-news',
templateUrl: './news.component.html',
styleUrls: ['./news.component.css']
})
export class NewsComponent implements OnInit { public news = [];
public newsTitle = '填写个人信息,添加到列表';
public currentMsg; constructor(private storage:MyDataService) { this.news = this.storage.getItem('msgList') || [];
} ngOnInit() { } addList() {
this.news.push(this.currentMsg);
this.storage.setItem('msgList',this.news);
}
delete(i){
this.news.splice(i,1);
this.storage.setItem('msgList',this.news);
} }

my-data1.sevice.ts:

import { Injectable } from '@angular/core';

@Injectable()
export class MyDataService { constructor() { } setItem(key,value){
localStorage.setItem(key,JSON.stringify(value));
}
getItem(key){
return JSON.parse(localStorage.getItem(key));
}
removeItem(key){
localStorage.removeItem(key);
} }
 

angular2.0---服务Service,使用服务进行数据处理的更多相关文章

  1. 安卓第十三天笔记-服务(Service)

    安卓第十三天笔记-服务(Service) Servcie服务 1.服务概念 服务 windows 服务没有界面,一直运行在后台, 运行在独立的一个进程里面 android 服务没有界面,一直运行在后台 ...

  2. Angular.js之服务与自定义服务学习笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service

    在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...

  4. Windows Service插件服务开源

    WindowsService 插件服务是一个为简化NTService开发和打包程序,提供插件开发的方式进行动态加入或删除业务. 插件式服务程序的由来,在系统维护的过程中,根据企业的要求经常要进行一些周 ...

  5. 安卓服务(Service)的两种开启方式以及服务的生命周期

    安卓中服务的开启方式 一:採用start的方式开启服务 调用函数:startService(Intent)->onCreate()->onStart()/onStartCommand()- ...

  6. 服务 Service 基本介绍

    Activity public class MainActivity extends ListActivity {     private boolean flag;//是否开启线程     publ ...

  7. Android服务Service总结

    转自 http://blog.csdn.net/liuhe688/article/details/6874378 富貴必從勤苦得,男兒須讀五車書.唐.杜甫<柏學士茅屋> 作为程序员的我们, ...

  8. Android学习笔记--服务(Service)

    1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...

  9. BZOJ 1820: [JSOI2010]Express Service 快递服务( dp )

    dp(i,j,k)表示在处理第i个业务, 另外2个在j,k处. 第一维可以滚动... --------------------------------------------------------- ...

随机推荐

  1. git 查看提交记录

    查看提交的内容 -p 选项,同时在 - 后加数字限制一下数目 git log -p -2. commit 500eeadd71a21f1166803e12a792bfa86f4ca784 (HEAD ...

  2. centos7启动顺序加密的问题

    在上一篇博客中我们说的是如何进入单用户模式,这篇我主要来讲centos7启动加密. 首先我们来说centos的启动顺序: 上一篇我们所说的进入单用户模式,就是在boot loader 这一层次下进入的 ...

  3. 【bzoj3028】 食物 生成函数+隔板法

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3028 这题的推导很妙啊,裸的推母函数的题. 我们首先构造出每种食物的母函数: 汉堡:$ ...

  4. POJ 2393

    #include <iostream> #include <algorithm> using namespace std; int main() { //freopen(&qu ...

  5. JS滑动到页面底部

    window.scrollTo(0, document.documentElement.clientHeight); 该 window 对象在DOM有一个 scrollTo 滚动到打开窗口 的任意位置 ...

  6. (转)python学习笔记5--decimal

    原文:https://blog.csdn.net/lemonwyc/article/details/37583125 上一节提到了除了基本类型之外的decimal,这节就学习下.查看python3.4 ...

  7. IIS web证书申请与安装

    数字证书一般是由权威机构颁发的,操作系统会携带权威机构的根证书和中级证书.如果操作系统没有携带权威机构签发的根证书,浏览器会报警,如www.12306.cn,需要安装铁道部根证书后,才能正常访问. 证 ...

  8. 【java排序】 选择排序,插入排序,希尔算法

    一.选择排序 1.基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换:然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止. 2.实例 3.算法 ...

  9. 初学者必知的Python中优雅的用法 分类: Python 2015-05-11 15:02 782人阅读 评论(0) 收藏

    本文由 伯乐在线 - 淘小米 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:freepythontips.wordpress.com.欢迎加入翻译小组. Hi 朋友们.由于我最近都比较忙 ...

  10. ELK日志系统之通用应用程序日志接入方案

    前边有两篇ELK的文章分别介绍了MySQL慢日志收集和Nginx访问日志收集,那么各种不同类型应用程序的日志该如何方便的进行收集呢?且看本文我们是如何高效处理这个问题的 日志规范 规范的日志存放路径和 ...