欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angular2 組件之間通過service互相傳遞的實例

 更新時間:2018年09月30日 09:21:09   作者:玩玩玩玩玩了  
今天小編就為大家分享一篇angular2 組件之間通過service互相傳遞的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

母組件傳值給子組件

母組件通過service傳值給子組件,很簡單,聲明一個service

@Injectable()
export class ToolbarTitleService {
 title:string;
}

然后在母組件中依賴注入

@Component({
 selector: 'admin',
 templateUrl: './admin.component.html',
 styleUrls: ['./admin.component.scss'],
 providers: [ToolbarTitleService],
})

子組件中直接聲明即可使用

export class RoleListComponent implements OnInit {
 constructor(private toolbarTitleService:ToolbarTitleService) {
   console.log(this.toolbarTitleService.title);
   }
 ngOnInit() { }
}

子組件傳值給母組件

那么我想反過來傳值回去該怎么辦,即使我在子組件注入了service,母組件也不會在我修改了servie的值之后得到通知,這時候就需要用到subscribe

service代碼:

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

@Injectable()
export class ToolbarTitleService {
 private titleSource = new Subject();
 //獲得一個Observable
 titleObservable =this.titleSource.asObservable();
 constructor() { }
 //發(fā)射數(shù)據(jù),當(dāng)調(diào)用這個方法的時候,Subject就會發(fā)射這個數(shù)據(jù),所有訂閱了這個Subject的Subscription都會接受到結(jié)果
 emitTitle(title: string) {
  this.titleSource.next(title);
 }
}

子組件代碼:

import { ToolbarTitleService } from './../../common/toolbar-title.service';
import { Component, OnInit ,Output,EventEmitter} from '@angular/core';

@Component({
 selector: 'role-list',
 templateUrl: 'role-list.component.html',
 styleUrls: ['./role-list.component.css'],
 providers: [],
})

export class RoleListComponent implements OnInit {
 constructor(private toolbarTitleService:ToolbarTitleService) {
   //調(diào)用方法,發(fā)射數(shù)據(jù)
   this.toolbarTitleService.emitTitle('角色列表');
   }
 ngOnInit() { }
}

母組件:

import { Component, OnInit } from '@angular/core';
import { ToolbarTitleService } from "app/common/toolbar-title.service";
import { Subscription } from "rxjs/Subscription";

@Component({
 selector: 'admin',
 templateUrl: './admin.component.html',
 styleUrls: ['./admin.component.scss'],
 providers: [ToolbarTitleService],
})

export class AdminComponent implements OnInit {
 title: string;
 subscription: Subscription;
 constructor(private toolbarTitleService: ToolbarTitleService) {
  //使用subscribe來訂閱,當(dāng)數(shù)據(jù)被發(fā)射出來的時候,這里就會接收到結(jié)果
  this.subscription = this.toolbarTitleService.titleObservable.subscribe(src => console.log('得到的title:' + src));

 }

 ngOnInit() {

 }
 //銷毀的時候需要取消訂閱,因為訂閱之后會一直處于觀察者狀態(tài),不取消會導(dǎo)致泄露
 ngOnDestroy() {
  this.subscription.unsubscribe();
 }
}

以上這篇angular2 組件之間通過service互相傳遞的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • AngularJS基礎(chǔ) ng-paste 指令簡單示例

    AngularJS基礎(chǔ) ng-paste 指令簡單示例

    本文主要介紹AngularJS ng-paste 指令,這里對ng-paste 指令的基礎(chǔ)資料做了整理,并附有代碼示例,有需要的朋友可以參考下
    2016-08-08
  • 深入理解Angular4中的依賴注入

    深入理解Angular4中的依賴注入

    在Angular中使用依賴注入,可以幫助我們實現(xiàn)松耦合,可以說只有在組件中使用依賴注入才能真正的實現(xiàn)可重用的組件。
    2017-06-06
  • AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解

    AngularJS基于factory創(chuàng)建自定義服務(wù)的方法詳解

    這篇文章主要介紹了AngularJS基于factory創(chuàng)建自定義服務(wù)的方法,結(jié)合實例形式分析了AngularJS使用factory創(chuàng)建自定義服務(wù)的具體步驟、操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2017-05-05
  • 詳解Angular中延遲加載的原理與使用

    詳解Angular中延遲加載的原理與使用

    Angular 是一個流行的框架,用于構(gòu)建動態(tài)和響應(yīng)式 Web 應(yīng)用程序,在本文中,我們將討論延遲加載以及它如何與 Angular 中的路由一起工作,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • AngularJS實現(xiàn)全選反選功能

    AngularJS實現(xiàn)全選反選功能

    這篇文章主要介紹了AngularJS實現(xiàn)全選反選功能,這里用到AngularJS四大特性之二----雙向數(shù)據(jù)綁定,對angularjs實現(xiàn)全選反選相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • AngularJS 應(yīng)用模塊化的使用

    AngularJS 應(yīng)用模塊化的使用

    這篇文章主要介紹了AngularJS 應(yīng)用模塊化的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解Angular路由動畫及高階動畫函數(shù)

    詳解Angular路由動畫及高階動畫函數(shù)

    本文主要講解了Angular的路由動畫和高階動畫函數(shù),對此感興趣的同學(xué),可以把代碼親自實驗一下,理解其原理。
    2021-05-05
  • AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法詳解

    AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法詳解

    這篇文章主要介紹了AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法,結(jié)合實例形式分析了AngularJS二維數(shù)組元素遍歷的相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • AngularJS指令用法詳解

    AngularJS指令用法詳解

    這篇文章主要介紹了AngularJS指令用法,較為詳細(xì)的分析了AngularJS指令的功能、用法及自定義指令的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-11-11
  • angularJS 指令封裝回到頂部示例詳解

    angularJS 指令封裝回到頂部示例詳解

    本篇文章主要介紹了angularJS 指令封裝回到頂部示例詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論