【问题标题】:Angular2 :Passing an object betwen 2 component via serviceAngular 2:通过服务在两个组件之间传递一个对象
【发布时间】:2016-07-12 16:55:12
【问题描述】:

我正在开发一个 Angular 2 beta9 应用程序,我正在尝试使组件相互处理,我有一个“开始”组件,它是一个单选框按钮,所选项目将被转移作为通过“目标”组件的服务的对象,它将使用此选定的项目来过滤它将使用的数据。

将对象从组件“start”传递给服务正常工作的问题:

FormeService.ts:44 
Object {id: 1, submenu: "type1", nb: 102, value: "Porte Fenêtre", class: ""…}

但是,从服务到组件“目标”,对象显示为未定义:undefined

这是我的代码,

开始组件:

@Component({
    selector: 'radioFormesType1',
    templateUrl: 'component1.html',
    styleUrls: ['component1.css'],

    directives: [MATERIAL_DIRECTIVES]



})
export class RadioFormesType1Component implements OnInit, OnDestroy{

    //valeur initiale du radio box
    data: any = {init: 'Fenêtre'};

    //liste parsèe du service
    items_list;


    constructor(public formeService: FormeService) {    }

    ngOnInit(){
        this.formeService.getjson().subscribe(people => this.items_list = people);
    }

    ngOnDestroy(){

    }

    public  onSelectType1(selected:any){
        console.log("valeur clickè "+selected.value);
        this.formeService.changeForme(selected);
        console.log("valeur selectionne passè au service"+selected.value);


    }

}

点击的动作在这里onSelectType1()

<div *ngFor="#item of items_list">
            <md-radio-button
                    *ngIf="item.id===1"
                    value="{{item.value}}"
                    class="{{item.class}}"
                    checked="{{item.checked}}"
                    (click)="onSelectType1(item)">
                {{item.label}}
            </md-radio-button>
        </div>
    </md-radio-group>

服务获取此对象并将其放置在名为“type1”的新对象中,这是我的服务的代码,默认加载json数据:

import {Injectable,EventEmitter,Output} from 'angular2/core';
import {Http} from "angular2/http";
import 'rxjs/add/operator/map';


@Injectable()
export class FormeService{

    /*valeur selectionnèe du type1*/
    private _type1:any;


    get type1():any {
        return this._type1;
    }

    set type1(value:any) {
        this._type1 = value;
    }


    constructor (public http : Http){

        //this.changeForme(selected)
        this.http= http;

    }

    /*parsing  */
    getjson(){
        return this.http.get('dev/JsonData/formes.json')
            .map(res => res.json())
    }



    /*actions*/

    /*recuperer le type1 : fenetre Ou Porte Fentre*/
    public  changeForme(selected):any{
        console.log(selected);

        this._type1=selected
        console.log("valeur type1 du service forme contient  "+this._type1);


        return this._type1;

    }

当我将它放入一个名为type1Recu 的新对象中时,该对象似乎处于此级别未定义的“目标组件”:

@Component({
    selector: 'checkboxFormesType2',
    templateUrl: 'component2.html',
    styleUrls: ['component2.css'],

    directives: [MATERIAL_DIRECTIVES,RadioFormesType1Component]
})
export  class CheckboxFormesType2 {

//liste parsèe du service
items_list;

// variable a en mettre le type1 selectionne
public  type1Recu: any;



constructor(public formeService: FormeService) {
    this.type1Recu = this.formeService.type1 ;
    console.log(this.formeService.type1)
    //console.log("type1Recu = " +this.type1Recu);

}

ngOnInit(){


    //chargement de la liste
    this.formeService.getjson().subscribe(people => this.items_list = people);
}

ngOnDestroy(){

}

关于能够在目标组件中加载整个对象的任何建议??

【问题讨论】:

    标签: typescript angular angular2-directives angular2-services


    【解决方案1】:

    如果没有您使用CheckboxFormesType2 的代码,我无法确定。但是,您似乎在实际定义 type1 之前构建 CheckboxFormesType2,例如在单击单选复选框之前。显而易见的解决方案是将*ngIf=type1" 属性添加到您的CheckboxFormesType2,如下所示:

    <checkboxFormesType2 *ngIf="type1"></checkboxFormesType2> 
    

    这样CheckboxFormesType2 在 type1 定义之前不会被创建。

    对于服务通信,我认为更好的方法是在您的服务中使用subject。对于您的代码,它将是这样的:

    表单服务:

    import {Injectable,EventEmitter,Output} from 'angular2/core';
    import {Http} from "angular2/http";
    import 'rxjs/add/operator/map';
    import {Subject} from 'rxjs/Rx';
    
    
    @Injectable()
    export class FormeService{
        public type1 = new Subject();
    ...
    }
    

    在 RadioFormesType1Component 中:

    export class RadioFormesType1Component implements OnInit, OnDestroy{
        ...
        public onSelectType1(selected:any){
            this.formeService.type1.next(selected);
        }
        ...
    }
    

    在 CheckboxFormesType2 中:

    export  class CheckboxFormesType2 {
        ...
        type1Recu:any;
        constructor(public formeService: FormeService) {
            this.formeService.type1.subscribe( type1 => this.type1Recu = type1);
        }
        ...
    }
    

    【讨论】:

    • 感谢您的回复,顺便说一句,我有一些标准要验证,因此依赖于所选的单选项目,服务会将特定数据加载到 chebbok 组件,这些数据只是属性和复选框项目的规格,
    • 为了更清楚,对于单选框的选择 A,我有 X 和 Y 和 Z 作为复选框的项目,对于收音机的选择 B,我有 Y,Z,V,复选框的 W 项,因此复选框组件的创建取决于我在收音机中选择的内容,并且这两个组件项都由服务通过 json 数据存储和解析。这正是目的
    • @firasKoubaa 您能否在问题中添加您拥有&lt;checkboxFormesType2&gt;&lt;/checkboxFormesType2&gt; 的组件的代码?
    • 我试图通过主题传递数据来解决这个问题,它给出了想要的结果,因此它似乎是在这种情况下处理对象的最佳选择。 (y) 非常感谢
    猜你喜欢
    • 2018-12-31
    • 2017-08-23
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2016-04-29
    • 2019-10-10
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多