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

angular4 共享服務(wù)在多個(gè)組件中數(shù)據(jù)通信的示例

 更新時(shí)間:2018年03月30日 11:01:17   作者:zxc19890923  
本篇文章主要介紹了angular4 共享服務(wù)在多個(gè)組件中數(shù)據(jù)通信的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

應(yīng)用場(chǎng)景,不同組件中操作統(tǒng)一組數(shù)據(jù),不論哪個(gè)組件對(duì)數(shù)據(jù)進(jìn)行了操作,其他組件中立馬看到效果。這樣他們就要共用一個(gè)服務(wù)實(shí)例,是本次的重點(diǎn),如果不同實(shí)例,那么操作的就不是同一組數(shù)據(jù),那么就不會(huì)有這樣的效果,想實(shí)現(xiàn)共用服務(wù)實(shí)例,就是在所有父組件中priviates:[]中引入這個(gè)組件,子組件中不需要再次引入,那么他們都是用的父組件中的服務(wù)實(shí)例。

1、公用服務(wù)

import {Injectable} from "@angular/core";

@Injectable()
export class CommonService {
 public dateList: any = [
 {
  name: "張旭超",
  age: 20,
  address: "北京市朝陽(yáng)區(qū)"
 }
 ];

 constructor() {

 }

 addDateFun(data) {
 this.dateList.push(data);
 }
}

2、parent.component.ts

import {Component, OnInit} from "@angular/core";
import {CommonService} from "./common.service";

// 這里要通過(guò)父子公用服務(wù)來(lái)操作數(shù)據(jù),只需要在父組件中引入服務(wù)。
@Component({
 selector: "parent-tag",
 templateUrl: "parent.component.html",
 providers: [
 CommonService
 ]
})
export class ParentComponent implements OnInit {
 public list: any = [];

 constructor(private commonService: CommonService) {
 this.list = commonService.dateList;
 }

 ngOnInit() {

 }
}

3、parent.component.html

<table width="500">
 <tr *ngFor="let item of list">
 <td>
  {{item.name}}
 </td>
 <td>
  {{item.age}}
 </td>
 <td>
  {{item.address}}
 </td>
 </tr>
</table>
<child-one-tag></child-one-tag>

4、child-one.component.ts

import {Component} from "@angular/core";
import {CommonService} from "./common.service";

@Component({
 selector: "child-one-tag",
 templateUrl: "child-one.component.html"
})
export class ChildOneComponent {
 public display: boolean = false;
 public username: string = "";
 public age: number = 20;
 public address: string = "";
 constructor(public commonService: CommonService) {

 }

 showDialog() {
 this.display = true;
 }

 hideDialog() {
 this.display = false;
 }

 addInfoFun() {
 let params = {
  name: this.username,
  age: this.age,
  address: this.address
 };
 this.commonService.addDateFun(params);
 params = {};
 }
}

5、child-one.component.html

<p-dialog header="彈窗" [(visible)]="display" [width]="300" appendTo="body" modal="modal">
 <form #myForm="ngForm" name="myForm">
 <p>姓名:<input type="text" name="username" [(ngModel)]="username" pInputText/></p>
 <p>年齡:<input type="number" name="age" [(ngModel)]="age" pInputText/></p>
 <p>地址:<input type="text" name="address" [(ngModel)]="address" pInputText/></p>
 <button pButton label="確定" type="submit" (click)="addInfoFun()"></button>
 <button pButton label="取消" (click)="hideDialog()"></button>
 </form>
</p-dialog>
<button label="添加" pButton (click)="showDialog()"></button>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談angularJS中的事件

    淺談angularJS中的事件

    下面小編就為大家?guī)?lái)一篇淺談angularJS中的事件。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • Angularjs使用directive自定義指令實(shí)現(xiàn)attribute繼承的方法詳解

    Angularjs使用directive自定義指令實(shí)現(xiàn)attribute繼承的方法詳解

    這篇文章主要介紹了Angularjs使用directive自定義指令實(shí)現(xiàn)attribute繼承的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了基于directive自定義指令實(shí)現(xiàn)attribute繼承的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • AngularJs  E2E Testing 詳解

    AngularJs E2E Testing 詳解

    本文主要介紹AngularJs E2E Testing的資料,這里整理了詳細(xì)的資料,及簡(jiǎn)單代碼示例,有興趣的小伙伴可以參考下
    2016-09-09
  • angularjs select 賦值 ng-options配置方法

    angularjs select 賦值 ng-options配置方法

    下面小編就為大家分享一篇angularjs select 賦值 ng-options配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • AngularJs 動(dòng)態(tài)加載模塊和依賴

    AngularJs 動(dòng)態(tài)加載模塊和依賴

    這篇文章主要介紹了AngularJs 動(dòng)態(tài)加載模塊和依賴方法的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Angular簡(jiǎn)單驗(yàn)證功能示例

    Angular簡(jiǎn)單驗(yàn)證功能示例

    這篇文章主要介紹了Angular簡(jiǎn)單驗(yàn)證功能,涉及AngularJS事件響應(yīng)、正則判定、頁(yè)面元素屬性動(dòng)態(tài)修改等相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • AngularJS基礎(chǔ)知識(shí)筆記之過(guò)濾器

    AngularJS基礎(chǔ)知識(shí)筆記之過(guò)濾器

    在我們開(kāi)發(fā)中經(jīng)常需要在頁(yè)面顯示給用戶的信息需要一定處理格式化,才能顯示給用戶。比如時(shí)間本地化,或者yyyy-MM-dd HH:mm:ss格式,數(shù)字精度格式化,本地化,人名格式化等等。在angularjs中為我們提供了叫filter的指令,讓我們能夠很輕易就能做到著一些列的功能
    2015-05-05
  • AngularJS實(shí)現(xiàn)動(dòng)態(tài)添加Option的方法

    AngularJS實(shí)現(xiàn)動(dòng)態(tài)添加Option的方法

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)動(dòng)態(tài)添加Option的方法,涉及AngularJS事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-05-05
  • angular使用TweenMax動(dòng)畫(huà)庫(kù)的問(wèn)題和解決方法

    angular使用TweenMax動(dòng)畫(huà)庫(kù)的問(wèn)題和解決方法

    這篇文章主要給大家介紹了關(guān)于angular使用TweenMax的相關(guān)問(wèn)題和解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • angularjs路由傳值$routeParams詳解

    angularjs路由傳值$routeParams詳解

    這篇文章主要為大家詳細(xì)介紹了angularjs路由傳值$routeParams的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評(píng)論