[Angular 2] Share a Service Across Angular 2 Components and Modules
Services are used to share data between components. They follow a module pattern that allows you to use the data throughout your application so that your data is consistent and in sync.
Create a service.module.ts:
import { NgModule} from '@angular/core';
import {SimpleService} from './simple.service';
@NgModule({
})
export class ServicesModule {
static forRoot(){
return {
ngModule: ServicesModule,
providers: [SimpleService]
}
}
}
export {
SimpleService
}
Simple.serivce.ts:
import { Injectable } from '@angular/core';
@Injectable()
export class SimpleService {
message = "Hello";
constructor() { }
}
app.module.ts:
import { NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {AppComponent} from './app.component';
import {HomeModule} from './components/home/home.module';
import {WidgetsModule} from './components/widgets/widgets.module';
import {ServicesModule} from './serivces/service.module';
@NgModule({
imports:[BrowserModule, HomeModule, WidgetsModule, ServicesModule.forRoot()],
declarations:[AppComponent],
bootstrap:[AppComponent]
})
export class AppModule{}
Use the SimpleService:
import { Component, OnInit } from '@angular/core';
import {SimpleService} from '../../serivces/service.module';
@Component({
moduleId: module.id,
selector: 'home',
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit {
constructor(private simpleService: SimpleService) {
console.log("message:", simpleService.message);
}
ngOnInit() { }
}
[Angular 2] Share a Service Across Angular 2 Components and Modules的更多相关文章
- angular 关于 factory、service、provider的相关用法
1.factory() Angular里面创建service最简单的方式是使用factory()方法. factory()让我们通过返回一个包含service方法和数据的对象来定义一个service. ...
- 使用Angular CDK实现一个Service弹出Toast组件
在Angular中,官方团队在开发Material组件库的同时,顺手做了一套Component dev kit,也就是在Angular世界中大名鼎鼎的CDK,这套工具包提供了非常多的前端开发的通用功能 ...
- [Angular 2 Router] Configure Your First Angular 2 Route
Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and ...
- angular核心原理解析1:angular自启动过程
angularJS的源代码整体上来说是一个自执行函数,在angularJS加载完成后,就会自动执行了. angular源代码中: angular = window.angular || (window ...
- Angular项目构建指南 - 不再为angular构建而犹豫不决(转)
如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余 ...
- 【Angular专题】——(1)Angular,孤傲的变革者
目录 一. 漫谈Angular 二. 如果你还在使用Angularjs 三. 我计划这样学习Angular技术栈 一. 漫谈Angular Angular,来自Google的前端SPA框架,与Reac ...
- Angular 从入坑到挖坑 - Angular 使用入门
一.Overview angular 入坑记录的笔记第一篇,完成开发环境的搭建,以及如何通过 angular cli 来创建第一个 angular 应用.入坑一个多星期,通过学习官方文档以及手摸手的按 ...
- [从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)
前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应 ...
- [Angular 2] Injecting a Service
Using Services in Angular 2 is very simple. This lesson covers how to create a simple class as a Ser ...
随机推荐
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Metasploit启动
一.启动Kali的PostgreSQL服务 由于使用PostgreSQL作为数据库,因此,必须先运行它. root@kali:~# service postgresql start 使用ss -ant ...
- 关于网站编码显示问题 效果是 访问 带有中文注释的sass文件出现编码报错。
首先查看环境变量 export declare -x HOME="/home/piperck" declare -x LANG="en_US.UTF-8" de ...
- 第三百五十四天 how can I 坚持
你的问题主要在于:读书不多而买书太多:读书太少又特爱思考,还话唠.. 2012年毕业,辗转无锡,上海,最后来到了北京,逛了北京, 2013年,清明去爬了长城,从天通苑搬到了甜水园, 2014年,爬了泰 ...
- 转】用Maven构建Mahout项目
原博文出自于: http://blog.fens.me/hadoop-mahout-maven-eclipse/ 感谢! 用Maven构建Mahout项目 Hadoop家族系列文章,主要介绍Hadoo ...
- 【转】log4j详解及简易搭建
原文链接:http://www.cnblogs.com/mailingfeng/archive/2011/07/28/2119937.html log4j是一个非常强大的log记录软件. 首先当然是得 ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- List小结
假设有两个List集合,找出集合中重复的部分: //检测listX和listY中的重复部分 //把X复制到Z避免循环同时操作X从而出现异常 itemX.ForEach(i = ...
- Oracle Standby Database 实现方案
Oracle Standby Database 实现方案 From: http://wanow.blog.hexun.com/4672755_d.html 字号:大 中 小 版本:V20060328 ...
- .net 学习资源(转)
名称:快速入门地址:http://chs.gotdotnet.com/quickstart/描述:本站点是微软.NET技术的快速入门网站,我们不必再安装.NET Framework中的快速入门示例 ...