angular 基本依赖注入
2018-04-22 11:46
190 查看
import { Injectable } from '@angular/core'; @Injectable() export class ProductServiceService { constructor() { } getProduct(): Product { return new Product(1, "iPhone7"); } } export class Product { constructor( public id: number, public title: string ) { } }
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { Product1Component } from './product1/product1.component'; import { ProductServiceService } from './shared/product-service.service'; @NgModule({ declarations: [ AppComponent, Product1Component ], imports: [ BrowserModule ], providers: [ProductServiceService], bootstrap: [AppComponent] }) export class AppModule { }
import { Component, OnInit } from '@angular/core'; import { ProductServiceService, Product } from '../shared/product-service.service'; @Component({ selector: 'app-product1', templateUrl: './product1.component.html', styleUrls: ['./product1.component.css'] }) export class Product1Component implements OnInit { product: Product; constructor(private productService: ProductServiceService) { } ngOnInit() { this.product = this.productService.getProduct(); } }
<p> 商品Id:{{product.id}} </p> <p> 商品描述:{{product.title}} </p>
相关文章推荐
- Angular之依赖注入(injector)与原生View组件
- Angular 理解module和injector,即依赖注入
- 【转】Javascript DI!Angular依赖注入的实现原理
- AngularJS $injector 依赖注入
- Angular-依赖注入
- Spring基本功能-依赖注入
- 用原生js简单模仿angular的依赖注入
- (七)理解angular中的module和injector,即依赖注入
- (5) 编码剖析Spring装配基本属性的原理【附加:注入依赖对象的两种方式】
- Angular 4 依赖注入学习教程之FactoryProvider的使用(四)
- Angular 4 依赖注入教程之一 依赖注入简介
- Angular4-在线竞拍应用-依赖注入
- Angular 依赖注入
- Angular 4依赖注入学习教程之FactoryProvider配置依赖对象(五)
- Angular 依赖注入
- Angular4 第四章 依赖注入基本概念
- 原创:Javascript DI!Angular依赖注入的实现原理
- Angular 4入门教程系列:6:Tour Of Heroes之服务和依赖注入
- Angular2 依赖注入
- angular依赖注入