最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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中ng-class的使用方法

    淺談AngularJS中ng-class的使用方法

    下面小編就為大家?guī)?lái)一篇淺談AngularJS中ng-class的使用方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • AngularJS出現(xiàn)$http異步后臺(tái)無(wú)法獲取請(qǐng)求參數(shù)問(wèn)題的解決方法

    AngularJS出現(xiàn)$http異步后臺(tái)無(wú)法獲取請(qǐng)求參數(shù)問(wèn)題的解決方法

    這篇文章主要介紹了AngularJS出現(xiàn)$http異步后臺(tái)無(wú)法獲取請(qǐng)求參數(shù)問(wèn)題的解決方法,較為詳細(xì)的分析了AngularJS出現(xiàn)異步請(qǐng)求后臺(tái)無(wú)法解析的原因與相應(yīng)的解決方法,需要的朋友可以參考下
    2016-11-11
  • 詳解Angular組件之投影

    詳解Angular組件之投影

    在html規(guī)范里面,它定義了非常多的標(biāo)簽,在這些標(biāo)簽里面,相同標(biāo)簽之間的嵌套,不同標(biāo)簽之間的嵌套,是十分常見,在Angular里面,我們可以通過(guò)自定義標(biāo)簽的方式引用組件,這里的標(biāo)簽?zāi)芊裣裨膆tml標(biāo)簽一樣,來(lái)嵌入html標(biāo)簽,或者嵌套其他組件標(biāo)簽?zāi)?本文將介紹投影的作用。
    2021-05-05
  • Angular2 路由問(wèn)題修復(fù)詳解

    Angular2 路由問(wèn)題修復(fù)詳解

    這篇文章主要介紹了Angular2 路由問(wèn)題修復(fù)詳解的相關(guān)資料,并建了一個(gè)測(cè)試工程,把詳細(xì)的過(guò)程分享給大家,需要的朋友可以參考下
    2017-03-03
  • angularJS結(jié)合canvas畫圖例子

    angularJS結(jié)合canvas畫圖例子

    這篇文章主要介紹了angularJS結(jié)合canvas畫圖例子的方法,需要的朋友可以參考下
    2015-02-02
  • AngularJS基于ui-route實(shí)現(xiàn)深層路由的方法【路由嵌套】

    AngularJS基于ui-route實(shí)現(xiàn)深層路由的方法【路由嵌套】

    這篇文章主要介紹了AngularJS基于ui-route實(shí)現(xiàn)深層路由的方法,涉及AngularJS路由嵌套操作相關(guān)實(shí)現(xiàn)步驟與技巧,需要的朋友可以參考下
    2016-12-12
  • 解決angular雙向綁定無(wú)效果,ng-model不能正常顯示的問(wèn)題

    解決angular雙向綁定無(wú)效果,ng-model不能正常顯示的問(wèn)題

    今天小編就為大家分享一篇解決angular雙向綁定無(wú)效果,ng-model不能正常顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • angular2倒計(jì)時(shí)組件使用詳解

    angular2倒計(jì)時(shí)組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了angular2倒計(jì)時(shí)組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Angular.JS中的指令引用template與指令當(dāng)做屬性詳解

    Angular.JS中的指令引用template與指令當(dāng)做屬性詳解

    這篇文章主要介紹了Angular.JS中的指令引用template與指令當(dāng)做屬性的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • angularjs指令中的compile與link函數(shù)詳解

    angularjs指令中的compile與link函數(shù)詳解

    這篇文章主要介紹了angularjs指令中的compile與link函數(shù)詳解,本文同時(shí)訴大家complie,pre-link,post-link的用法與區(qū)別等內(nèi)容,需要的朋友可以參考下
    2014-12-12

最新評(píng)論

图片| 泾川县| 封丘县| 汉川市| 奇台县| 南陵县| 顺平县| 建昌县| 通榆县| 宜宾市| 徐闻县| 梧州市| 边坝县| 南丰县| 无锡市| 叶城县| 克山县| 礼泉县| 汉源县| 郯城县| 元氏县| 清原| 惠水县| 新龙县| 平阳县| 孝义市| 清新县| 抚远县| SHOW| 东明县| 海盐县| 如皋市| 天门市| 阿坝县| 吴旗县| 临西县| 湘潭市| 南江县| 项城市| 邻水| 大足县|