[Angular 2] 5. Inject Service with "Providers"

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.

import {Component} from ‘angular2/core‘;
import {TodoService} from ‘./../services/TodoService‘;

@Component({
    selector: ‘todos‘,
    providers: [TodoService],
    template: `
        <div>
            <ul>
                <li *ngFor="#todo of todoService.todos
                    {{todo.name}}
                </li>
            </ul>
        </div>
    `
})

export class TodoList implements OnInit{

    todos: Array<any>

    constructor(private todoService: TodoService){}

    ngOnInit(){
        this.todos = this.todoService.getTodos();
    }
}
export class TodoService {
    todos = [
        {id: 0, name: "eat"},
        {id: 1, name: "sleep"},
        {id: 2, name: "running"},
    ];

    getTodos(){
        return this.todos;
    }
}

Here we use providers, which tell Angular, we want to use TodoService, create a instance for us, and so that we can use the instance.

providers: [TodoService],

Actually, this is shorthard syntax, you can do:

providers: [
 { provider: TodoService, useClass: TodoService}
],

Here need to make sure, "provider" ‘s token are the same use "userClass", if you change "userClass" to some other service, it will still use the "TodoService":

providers: [
 { provide: TodoService, useClass: OtherService} // still use TodoService instead
],

To recap importing a data really just makes the type available, but doesn‘t give us an instance. In order to inject objects, we need providers that know how to create these objects by a given token which is a type.

时间: 2024-08-07 08:38:00

[Angular 2] 5. Inject Service with "Providers"的相关文章

[Angular 2] Inject Service

TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you want to Inject a Service without using TypeScript, you’ll need to understand the @Inject decorator. import {Component, View, Inject} from "angular2/angul

[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/co

[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

Guice 学习(六)使用Provider注入服务( Provider Inject Service)

1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Service { public void execute(); } 2.定义实现类 package com.guice.providerInject; public class OneService implements Service { @Override public void execute() {

[Angular 2] 9. Value Providers &amp; @Inject

Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really want is inject a simple value, which can be a primitive, or maybe just a configuration object. For these cases, we can use value providers and in this

[Angular] Difference between Providers and ViewProviders

For example we have a component: class TodoList { private todos: Todo[] = []; add(todo: Todo) {} remove(todo: Todo) {} set(todo: Todo, index: number) {} get(index: number) {} getAll() {} } @Component({ // ... viewProviders: [TodoList] // ... }) class

Angular 学习笔记——service &amp;constant

<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></scrip

Angular基础(六) DI

  一.依赖注入 a) 如果模块A需要依赖模块B,通常的做法是在A中导入B,import{B} from 'B',但有一些场合需要解除这种直接依赖,比如单元测试时需要mock一个B对象.还有时要创建B的单例或者用工厂模式生成B,这时适合使用依赖注入(Dependency Injection)的方式来解除对B的直接依赖. b) Angular的依赖注入框架包含三部分,Provider.Injector.Dependency,三者的关系为: Provider的作用是建立类与其依赖项之间的映射(map

Understanding Service Types

Last update: June 2014. I have partially rewritten this article to provide more technical details and also to show their differences more clearly. Angular comes with different types of services. Each one with its own use cases. Something important th