检索从第二个组件中的json获取传递的数组时,我得到[object Object]

时间:2016-11-11 08:58:18

标签: arrays json angular

这是我将这些东西推送到数组命名项目的第一个组件,我试图通过服务在第二个组件中获取

import { Component } from '@angular/core';
import { FormBuilder, FormControl } from '@angular/forms'; 
import {AppService} from '../second/app.service';
import { Router }   from '@angular/router';
import { Http,Response } from '@angular/http';
import { routing, appRoutingProviders } from '../app.route';
import { Validators } from '@angular/forms';
import {BrowserModule} from '@angular/platform-browser';

@Component({

  selector: 'first-app',
  templateUrl:"../app/src/component/first/app.firstpage.html"             
          })

export class FirstComponent 
{      
    data:any;      
    public items=[];
    public edited=false;
    public city=false;
    public dateCon=false;

    inputForm: FormGroup;
    Select: FormControl;
    Selectt: FormControl;
    dat:FormControl;

  constructor(private appservice:AppService,builder: FormBuilder, router:Router)     
  {
              this.appservice.getData().subscribe(res=>{this.data=res.json()});
              console.log(this.data);
              this.Select = new FormControl('', [
                Validators.required
                                                  ]);
              this.Selectt = new FormControl('', [
                Validators.required
                                                ]);
              this.dat = new FormControl('', [
                Validators.required
                                                  ]);
              this.inputForm = builder.group({
              Select: this.Select,
              Selectt: this.Selectt,
              dat: this.dat
                                            });
              this.router=router;
              this.appservice=appservice;    
          } 

   ngOnInit(){
           this.appservice.getData()
             }

     onclick(a,b) {
          console.log(this.data);
          let sel1=this.inputForm.value.Select;
          let sel2=this.inputForm.value.Selectt;
          let sel3=this.inputForm.value.dat;
          console.log(sel3);
          console.log(sel1);
          console.log(sel2);
          console.log(this.data.bus.length);
            for(let i=0;i<this.data.bus.length;i++){

              if((this.data.bus[i].from==sel1)&&(this.data.bus[i].to==sel2))
                                        {
                     this.items.push(this.data.bus[i]);     
                                        }
                                                   }
                        this.appservice.setData(this.items);
                                                    }

                  if((sel1!="")&&(sel2!="")&&(sel3!="")&&(sel1!=sel2))
                  {
                          this.router.navigate(['/sec-component']);
                  }
                  else if((sel1=="")&&(sel2=="")&&(sel3==""))
                    {
                          this.edited=true;
                    }
                  if((sel1==sel2)&&((sel1!="")&&(sel2!="")))
                    {
                            this.edited=false;
                            this.city=true;
                    }
                  else 
                   {
                          this.city=false;
                     }
                  if(sel1!=sel2)
                     {
                          this.edited=false;
                     }
                  if(sel3=="")
                       {
                            this.dateCon=true;
                       }
                  else  
                   {        
                            this.dateCon=false;
                       }
 }       
}

这是我传递此数组的第二个组件,我需要在那里打印并且要访问每个属性而不是整个内容。

    import { Component } from '@angular/core';
    import {AppService} from '../first/first.service';


@Component({
 template:
 `
        <h1>second component</h1>
        <h1>second component</h1>
        <p >{{myName}}</p>
 `
           })

export class SecondComponent {

    constructor(private appservice: AppService)
                    {
                        this.appservice=appservice;
                        this.myName=appservice.getVal();    
                    }

                            }

这是我返回值的服务页面

 import {Component, Injectable,Input,Output,EventEmitter} from '@angular/core'
 import { Http, Response } from '@angular/http';

export interface myData 
{
   name:any;
}

@Injectable()
export class AppService 
{
        sharingData: myData={name:""};
        constructor(private http:Http){ }
                 getData()
                        {
                            return this.http.get('./app/src/component/busDet.json')
                        }

                setData(i)
                         {
                             console.log('save data function called' + i + this.sharingData.name);
                             this.sharingData.name=i; 
                             console.log(this.sharingData.name);
                         }
                getVal()
                        { 
                            console.log(this.sharingData.name);
                            return this.sharingData.name;
                        }

}

我将输出作为object.object 我无法在下一个组件的JSON中获取值。

0 个答案:

没有答案