Hi I tried communication between components through services in angular 2 but returning function returns empty values -


second component of page need values first component

    import { component } '@angular/core';     import {appservice} './app.service';     import { http,response } '@angular/http';      @component({  template:  `         <h1>second component</h1>         <h1>second component</h1>         <p>{{myname}}</p>  `,  providers:[appservice]             })  export class secondcomponent {            constructor(private appservice: appservice)                     {                         this.appservice=appservice;                         this.myname=this.appservice.getdat();                         console.log(this.myname);                            }                                 } 

** first component page need pass value second component**

import { component } '@angular/core'; import { formbuilder, formcontrol } '@angular/forms';  import {appservice} '../second/app.service'; import { router }   '@angular/router'; import { http,response } '@angular/http'; import { routing, approutingproviders } '../app.route'; import { validators } '@angular/forms'; import {browsermodule} '@angular/platform-browser';  @component({    selector: 'first-app',   templateurl:"../app/src/component/first/app.firstpage.html",   providers:[appservice]                         })  export class firstcomponent  {           data:any;            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.appservice.setdata(this.data.bus[i]);*/                            this.appservice.setdata(i);                       console.log(i);                                             }                                                     }                    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;                        }  }        } 

service.ts values not getting reflected in below return function sharingdata.name getting empty values on consoling it.

import {component, injectable,input,output,eventemitter} '@angular/core' import { http, response } '@angular/http';  export interface mydata  {    name:number; }  @injectable() export class appservice  {            sharingdata: mydata={name};                 setdata(i)                          {                              console.log('save data function called' + + this.sharingdata.name);                              this.sharingdata.name=i;                               console.log(this.sharingdata.name);                          }                  getdat()                         {                             console.log(this.sharingdata.name);                             return this.sharingdata.name;                         }          constructor(private http:http){}                   getdata()                         {                             return this.http.get('./app/src/component/busdet.json')                          }     } 

module.ts

import { ngmodule }       '@angular/core'; import { browsermodule }  '@angular/platform-browser'; import { httpmodule }    '@angular/http'; import {appservice} './second/app.service'; import { reactiveformsmodule } '@angular/forms';  import { formsmodule }    '@angular/forms'; import { routing, approutingproviders } './app.route'; import { appcomponent }    './app.component'; import { firstcomponent }  './first/first.component'; import { secondcomponent }  './second/second.component'; import { thirdcomponent }  './third/third.component'; import { jsonpmodule } '@angular/http';  @ngmodule({   imports: [     browsermodule,     formsmodule,reactiveformsmodule,httpmodule,jsonpmodule,     routing   ],   declarations: [     appcomponent,     firstcomponent,     secondcomponent,     thirdcomponent    ],   providers: [     approutingproviders, appservice   ],   bootstrap: [ appcomponent ] })  export class appmodule { } 

register service in application module (common module components), , remove registration components.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -