Common Service for multiple module





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Can someone help me out fixing this issue?
I have a service which has multiple methods each method perform some task after receiving the response from the server and then I have to pass the modified response to the respective caller component.
this is the code I have in the service.



 getData(observer){
// prepare request Object
this.httpService.getDropdownData(reqObj).subscribe(
(data) => {

// modify response and then pass it to the respective component.
//I can't think of any solution from here. tried adding
observable but not getting the desired output(scoping issue)


observer.next(data);
observer.complete();
}, (error) => {
// error case
});
}

public observeData = new Observable(getData);


// component code.

this.cmpService.observeData().subscribe( response){
do something with the response.
};

expecting a modified output in each component.









share|improve this question























  • Please post all the code for your service(s) and your component.

    – Andrew Felder
    Jan 4 at 13:44


















1















Can someone help me out fixing this issue?
I have a service which has multiple methods each method perform some task after receiving the response from the server and then I have to pass the modified response to the respective caller component.
this is the code I have in the service.



 getData(observer){
// prepare request Object
this.httpService.getDropdownData(reqObj).subscribe(
(data) => {

// modify response and then pass it to the respective component.
//I can't think of any solution from here. tried adding
observable but not getting the desired output(scoping issue)


observer.next(data);
observer.complete();
}, (error) => {
// error case
});
}

public observeData = new Observable(getData);


// component code.

this.cmpService.observeData().subscribe( response){
do something with the response.
};

expecting a modified output in each component.









share|improve this question























  • Please post all the code for your service(s) and your component.

    – Andrew Felder
    Jan 4 at 13:44














1












1








1








Can someone help me out fixing this issue?
I have a service which has multiple methods each method perform some task after receiving the response from the server and then I have to pass the modified response to the respective caller component.
this is the code I have in the service.



 getData(observer){
// prepare request Object
this.httpService.getDropdownData(reqObj).subscribe(
(data) => {

// modify response and then pass it to the respective component.
//I can't think of any solution from here. tried adding
observable but not getting the desired output(scoping issue)


observer.next(data);
observer.complete();
}, (error) => {
// error case
});
}

public observeData = new Observable(getData);


// component code.

this.cmpService.observeData().subscribe( response){
do something with the response.
};

expecting a modified output in each component.









share|improve this question














Can someone help me out fixing this issue?
I have a service which has multiple methods each method perform some task after receiving the response from the server and then I have to pass the modified response to the respective caller component.
this is the code I have in the service.



 getData(observer){
// prepare request Object
this.httpService.getDropdownData(reqObj).subscribe(
(data) => {

// modify response and then pass it to the respective component.
//I can't think of any solution from here. tried adding
observable but not getting the desired output(scoping issue)


observer.next(data);
observer.complete();
}, (error) => {
// error case
});
}

public observeData = new Observable(getData);


// component code.

this.cmpService.observeData().subscribe( response){
do something with the response.
};

expecting a modified output in each component.






angular rxjs observable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 13:28









NitinNitin

115




115













  • Please post all the code for your service(s) and your component.

    – Andrew Felder
    Jan 4 at 13:44



















  • Please post all the code for your service(s) and your component.

    – Andrew Felder
    Jan 4 at 13:44

















Please post all the code for your service(s) and your component.

– Andrew Felder
Jan 4 at 13:44





Please post all the code for your service(s) and your component.

– Andrew Felder
Jan 4 at 13:44












2 Answers
2






active

oldest

votes


















0














It should better be something like:



service:



getData(): Observable<any> {
return this.httpService.getDropdownData(reqObj).pipe(
// do your stuffs here with the help of rxjs operators
);
}


component:



this.cmpService.getData().subscribe(
data => console.log(data), // here you'll get modified datas
err => console.log(err), // here, you'll manage errors in your component
() => {}
);


Hope it helps ;-)






share|improve this answer



















  • 1





    let me try this. thanks

    – Nitin
    Jan 4 at 17:06



















0














this is what I have tried and able to achieve my goal but not sure what performance impact it has and if this is the correct way of using observable?



      getCmpData(): Observable<any>{

this.spinner.show();
let userGroups =
if(userData != null){
userData = JSON.parse(userData);
if(userData.user && userData.user.userGroups &&
userData.user.userGroups.length){
userGroups = userData.user.userGroups;
}
}
return Observable.create(observer => {
this.httpService.getData('cmpInfo/', userGroups)
//.map(res => res.json())getting ts error so commented out
.subscribe((data) => {
if (data[0].status != 'ERROR') {
observer.next(data[0].results);// each component has different logic to use this data.
observer.complete();
}else{
this.spinner.hide();
this.modalService.showErrorMessage();
observer.unsubscribe();
}

}, (error) => {
this.spinner.hide();
this.modalService.showErrorMessage();
observer.unsubscribe();
});
});
}
}


//component code.



this.cmpService.getCmpData().subscribe(
(data) => {
// further logic
});





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039877%2fcommon-service-for-multiple-module%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It should better be something like:



    service:



    getData(): Observable<any> {
    return this.httpService.getDropdownData(reqObj).pipe(
    // do your stuffs here with the help of rxjs operators
    );
    }


    component:



    this.cmpService.getData().subscribe(
    data => console.log(data), // here you'll get modified datas
    err => console.log(err), // here, you'll manage errors in your component
    () => {}
    );


    Hope it helps ;-)






    share|improve this answer



















    • 1





      let me try this. thanks

      – Nitin
      Jan 4 at 17:06
















    0














    It should better be something like:



    service:



    getData(): Observable<any> {
    return this.httpService.getDropdownData(reqObj).pipe(
    // do your stuffs here with the help of rxjs operators
    );
    }


    component:



    this.cmpService.getData().subscribe(
    data => console.log(data), // here you'll get modified datas
    err => console.log(err), // here, you'll manage errors in your component
    () => {}
    );


    Hope it helps ;-)






    share|improve this answer



















    • 1





      let me try this. thanks

      – Nitin
      Jan 4 at 17:06














    0












    0








    0







    It should better be something like:



    service:



    getData(): Observable<any> {
    return this.httpService.getDropdownData(reqObj).pipe(
    // do your stuffs here with the help of rxjs operators
    );
    }


    component:



    this.cmpService.getData().subscribe(
    data => console.log(data), // here you'll get modified datas
    err => console.log(err), // here, you'll manage errors in your component
    () => {}
    );


    Hope it helps ;-)






    share|improve this answer













    It should better be something like:



    service:



    getData(): Observable<any> {
    return this.httpService.getDropdownData(reqObj).pipe(
    // do your stuffs here with the help of rxjs operators
    );
    }


    component:



    this.cmpService.getData().subscribe(
    data => console.log(data), // here you'll get modified datas
    err => console.log(err), // here, you'll manage errors in your component
    () => {}
    );


    Hope it helps ;-)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 4 at 14:18









    dun32dun32

    47535




    47535








    • 1





      let me try this. thanks

      – Nitin
      Jan 4 at 17:06














    • 1





      let me try this. thanks

      – Nitin
      Jan 4 at 17:06








    1




    1





    let me try this. thanks

    – Nitin
    Jan 4 at 17:06





    let me try this. thanks

    – Nitin
    Jan 4 at 17:06













    0














    this is what I have tried and able to achieve my goal but not sure what performance impact it has and if this is the correct way of using observable?



          getCmpData(): Observable<any>{

    this.spinner.show();
    let userGroups =
    if(userData != null){
    userData = JSON.parse(userData);
    if(userData.user && userData.user.userGroups &&
    userData.user.userGroups.length){
    userGroups = userData.user.userGroups;
    }
    }
    return Observable.create(observer => {
    this.httpService.getData('cmpInfo/', userGroups)
    //.map(res => res.json())getting ts error so commented out
    .subscribe((data) => {
    if (data[0].status != 'ERROR') {
    observer.next(data[0].results);// each component has different logic to use this data.
    observer.complete();
    }else{
    this.spinner.hide();
    this.modalService.showErrorMessage();
    observer.unsubscribe();
    }

    }, (error) => {
    this.spinner.hide();
    this.modalService.showErrorMessage();
    observer.unsubscribe();
    });
    });
    }
    }


    //component code.



    this.cmpService.getCmpData().subscribe(
    (data) => {
    // further logic
    });





    share|improve this answer




























      0














      this is what I have tried and able to achieve my goal but not sure what performance impact it has and if this is the correct way of using observable?



            getCmpData(): Observable<any>{

      this.spinner.show();
      let userGroups =
      if(userData != null){
      userData = JSON.parse(userData);
      if(userData.user && userData.user.userGroups &&
      userData.user.userGroups.length){
      userGroups = userData.user.userGroups;
      }
      }
      return Observable.create(observer => {
      this.httpService.getData('cmpInfo/', userGroups)
      //.map(res => res.json())getting ts error so commented out
      .subscribe((data) => {
      if (data[0].status != 'ERROR') {
      observer.next(data[0].results);// each component has different logic to use this data.
      observer.complete();
      }else{
      this.spinner.hide();
      this.modalService.showErrorMessage();
      observer.unsubscribe();
      }

      }, (error) => {
      this.spinner.hide();
      this.modalService.showErrorMessage();
      observer.unsubscribe();
      });
      });
      }
      }


      //component code.



      this.cmpService.getCmpData().subscribe(
      (data) => {
      // further logic
      });





      share|improve this answer


























        0












        0








        0







        this is what I have tried and able to achieve my goal but not sure what performance impact it has and if this is the correct way of using observable?



              getCmpData(): Observable<any>{

        this.spinner.show();
        let userGroups =
        if(userData != null){
        userData = JSON.parse(userData);
        if(userData.user && userData.user.userGroups &&
        userData.user.userGroups.length){
        userGroups = userData.user.userGroups;
        }
        }
        return Observable.create(observer => {
        this.httpService.getData('cmpInfo/', userGroups)
        //.map(res => res.json())getting ts error so commented out
        .subscribe((data) => {
        if (data[0].status != 'ERROR') {
        observer.next(data[0].results);// each component has different logic to use this data.
        observer.complete();
        }else{
        this.spinner.hide();
        this.modalService.showErrorMessage();
        observer.unsubscribe();
        }

        }, (error) => {
        this.spinner.hide();
        this.modalService.showErrorMessage();
        observer.unsubscribe();
        });
        });
        }
        }


        //component code.



        this.cmpService.getCmpData().subscribe(
        (data) => {
        // further logic
        });





        share|improve this answer













        this is what I have tried and able to achieve my goal but not sure what performance impact it has and if this is the correct way of using observable?



              getCmpData(): Observable<any>{

        this.spinner.show();
        let userGroups =
        if(userData != null){
        userData = JSON.parse(userData);
        if(userData.user && userData.user.userGroups &&
        userData.user.userGroups.length){
        userGroups = userData.user.userGroups;
        }
        }
        return Observable.create(observer => {
        this.httpService.getData('cmpInfo/', userGroups)
        //.map(res => res.json())getting ts error so commented out
        .subscribe((data) => {
        if (data[0].status != 'ERROR') {
        observer.next(data[0].results);// each component has different logic to use this data.
        observer.complete();
        }else{
        this.spinner.hide();
        this.modalService.showErrorMessage();
        observer.unsubscribe();
        }

        }, (error) => {
        this.spinner.hide();
        this.modalService.showErrorMessage();
        observer.unsubscribe();
        });
        });
        }
        }


        //component code.



        this.cmpService.getCmpData().subscribe(
        (data) => {
        // further logic
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 6 at 7:37









        NitinNitin

        115




        115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039877%2fcommon-service-for-multiple-module%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas