[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() { }

}
时间: 2024-12-26 08:38:51

[Angular 2] Share a Service Across Angular 2 Components and Modules的相关文章

[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 then importing the configured RouterModule into your main App Module. Use the Wiki Search as example project. Create a HomeComponent to contain every

angular核心原理解析1:angular自启动过程

angularJS的源代码整体上来说是一个自执行函数,在angularJS加载完成后,就会自动执行了. angular源代码中: angular = window.angular || (window.angular = {}) 定义一个全局的angular空对象. 然后: bindJQuery(); //绑定jQuery publishExternalAPI(angular); //扩展angular对象的方法和属性 jqLite(document).ready(function() { an

Angular项目构建指南 - 不再为angular构建而犹豫不决(转)

如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余前端框架最大的不同,在于它的核心不再是DOM,而是数据,是model.我们惯用的不管是单纯的jQuery还是MVC的Backbone,它们本质仍是让我们更方便更有条理的操作DOM,但是Angular不是.通过一系列魔术般的手法,它将一切的重心转移到数据上.以开发应用而不是操作节点的方式去开发Web,

[从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应用. 需求 需求大概是这样的: 开一个新的 Angular 项目,并且一开始选择加入 Router 功能 根组件是 AppComponent ,然后下方有三个子组件分别是 page1 page2 page3 可以在 AppComponent 内点击连结切换到这三个页面 参考文档: 路由与导航 Rou

[Angular Directive] Create a Template Storage Service in Angular 2

You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. You can store these TemplateRefs in a Service and then access them from any @Directive or @Component in your app. We want to create a service and a componen

[Angular 2] 5. Inject Service with &quot;Providers&quot;

In this lesson, we’re going to take a look at how add a class to the providers property of a component creates an actual providers. We’ll learn what a provider specifically does and how we can provide different dependencies with the same token. impor

[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2

A @Directive is used to add behavior to elements and components in your application. This makes @Directives ideal for behaviors such as "tracking" which don't belong in a Component, but do belong as a behavior in your application. import {Direct

[Angular 2] Injecting a Service

Using Services in Angular 2 is very simple. This lesson covers how to create a simple class as a Service then set it up so that you can use it across all your Components. In the bootstrap() we can inject the service to the application. Then the servi

[Angular 2] Share Template Content In Another Template With Content Projection &lt;ng-content&gt;

Angular 1 provided a mechanism to place content from your template inside of another template called transclusion. This concept has been brought into Angular 2 and was renamed to content projection and given super powers. In this lesson learn how to