Angular 6 add items into Observable










0














I'm new to Angular 6 and i'm having trouble on how i can add objects into observable in a service.



i have this observable



 getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');



and i need to add an item into that observable via another function



addContact(item)
//observable insertion goes here.



Here is my full service code



export class ContactService 

contact;
details;

constructor(private http: HttpClient)

getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');


addContact(contactName: string, contactPhone: string)













share|improve this question























  • seems like you need to use RxJS forkJoin
    – Suresh Kumar Ariya
    Nov 12 at 10:34






  • 1




    I do not understand, what you exactly want to achieve. Observables only support you to get messages from your server, they do not directly contain the data themselves. You only get your contact/user data when you subscribe() on the observable. The docs explain it better than I do (angular.io/guide/observables). Anyway, you probably want to add an item to your contact?
    – gillesB
    Nov 12 at 10:41






  • 1




    @gillesB Yes, Exactly that's what i wanted but add contact only on the current instance not in the server
    – Francis Rubia
    Nov 12 at 10:43










  • It is hard to read this as a comment. Please edit your answer and put the code there.
    – gillesB
    Nov 12 at 10:51






  • 1




    @gillesB i edit my question. last part is my full service code
    – Francis Rubia
    Nov 12 at 10:57















0














I'm new to Angular 6 and i'm having trouble on how i can add objects into observable in a service.



i have this observable



 getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');



and i need to add an item into that observable via another function



addContact(item)
//observable insertion goes here.



Here is my full service code



export class ContactService 

contact;
details;

constructor(private http: HttpClient)

getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');


addContact(contactName: string, contactPhone: string)













share|improve this question























  • seems like you need to use RxJS forkJoin
    – Suresh Kumar Ariya
    Nov 12 at 10:34






  • 1




    I do not understand, what you exactly want to achieve. Observables only support you to get messages from your server, they do not directly contain the data themselves. You only get your contact/user data when you subscribe() on the observable. The docs explain it better than I do (angular.io/guide/observables). Anyway, you probably want to add an item to your contact?
    – gillesB
    Nov 12 at 10:41






  • 1




    @gillesB Yes, Exactly that's what i wanted but add contact only on the current instance not in the server
    – Francis Rubia
    Nov 12 at 10:43










  • It is hard to read this as a comment. Please edit your answer and put the code there.
    – gillesB
    Nov 12 at 10:51






  • 1




    @gillesB i edit my question. last part is my full service code
    – Francis Rubia
    Nov 12 at 10:57













0












0








0







I'm new to Angular 6 and i'm having trouble on how i can add objects into observable in a service.



i have this observable



 getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');



and i need to add an item into that observable via another function



addContact(item)
//observable insertion goes here.



Here is my full service code



export class ContactService 

contact;
details;

constructor(private http: HttpClient)

getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');


addContact(contactName: string, contactPhone: string)













share|improve this question















I'm new to Angular 6 and i'm having trouble on how i can add objects into observable in a service.



i have this observable



 getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');



and i need to add an item into that observable via another function



addContact(item)
//observable insertion goes here.



Here is my full service code



export class ContactService 

contact;
details;

constructor(private http: HttpClient)

getContacts()
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');


addContact(contactName: string, contactPhone: string)










angular typescript observable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:51

























asked Nov 12 at 10:31









Francis Rubia

497




497











  • seems like you need to use RxJS forkJoin
    – Suresh Kumar Ariya
    Nov 12 at 10:34






  • 1




    I do not understand, what you exactly want to achieve. Observables only support you to get messages from your server, they do not directly contain the data themselves. You only get your contact/user data when you subscribe() on the observable. The docs explain it better than I do (angular.io/guide/observables). Anyway, you probably want to add an item to your contact?
    – gillesB
    Nov 12 at 10:41






  • 1




    @gillesB Yes, Exactly that's what i wanted but add contact only on the current instance not in the server
    – Francis Rubia
    Nov 12 at 10:43










  • It is hard to read this as a comment. Please edit your answer and put the code there.
    – gillesB
    Nov 12 at 10:51






  • 1




    @gillesB i edit my question. last part is my full service code
    – Francis Rubia
    Nov 12 at 10:57
















  • seems like you need to use RxJS forkJoin
    – Suresh Kumar Ariya
    Nov 12 at 10:34






  • 1




    I do not understand, what you exactly want to achieve. Observables only support you to get messages from your server, they do not directly contain the data themselves. You only get your contact/user data when you subscribe() on the observable. The docs explain it better than I do (angular.io/guide/observables). Anyway, you probably want to add an item to your contact?
    – gillesB
    Nov 12 at 10:41






  • 1




    @gillesB Yes, Exactly that's what i wanted but add contact only on the current instance not in the server
    – Francis Rubia
    Nov 12 at 10:43










  • It is hard to read this as a comment. Please edit your answer and put the code there.
    – gillesB
    Nov 12 at 10:51






  • 1




    @gillesB i edit my question. last part is my full service code
    – Francis Rubia
    Nov 12 at 10:57















seems like you need to use RxJS forkJoin
– Suresh Kumar Ariya
Nov 12 at 10:34




seems like you need to use RxJS forkJoin
– Suresh Kumar Ariya
Nov 12 at 10:34




1




1




I do not understand, what you exactly want to achieve. Observables only support you to get messages from your server, they do not directly contain the data themselves. You only get your contact/user data when you subscribe() on the observable. The docs explain it better than I do (angular.io/guide/observables). Anyway, you probably want to add an item to your contact?
– gillesB
Nov 12 at 10:41




I do not understand, what you exactly want to achieve. Observables only support you to get messages from your server, they do not directly contain the data themselves. You only get your contact/user data when you subscribe() on the observable. The docs explain it better than I do (angular.io/guide/observables). Anyway, you probably want to add an item to your contact?
– gillesB
Nov 12 at 10:41




1




1




@gillesB Yes, Exactly that's what i wanted but add contact only on the current instance not in the server
– Francis Rubia
Nov 12 at 10:43




@gillesB Yes, Exactly that's what i wanted but add contact only on the current instance not in the server
– Francis Rubia
Nov 12 at 10:43












It is hard to read this as a comment. Please edit your answer and put the code there.
– gillesB
Nov 12 at 10:51




It is hard to read this as a comment. Please edit your answer and put the code there.
– gillesB
Nov 12 at 10:51




1




1




@gillesB i edit my question. last part is my full service code
– Francis Rubia
Nov 12 at 10:57




@gillesB i edit my question. last part is my full service code
– Francis Rubia
Nov 12 at 10:57












2 Answers
2






active

oldest

votes


















1














If this.contacts is an Observable of list of objects (contacts: Observable<Items>) and you want to make some changes to that list, you can simply use map like this:



import map from 'rxjs/operators';

this.contacts.pipe(map(usersList =>
usersList.push(newItem);
return usersList;
));


But if you want to make another request to the server and merge this lists, you can use forkJoin like this:



import forkJoin from 'rxjs';

forkJoin(
this.contacts,
this.http.get('https://jsonplaceholder.typicode.com/other_users');
).pipe(
map(data =>
const currentResult = data[0];
const pastResult = data[1];
// ...

));


Update



Based on the your comment for more details, you don't need to do anything with observables. You need is something like this:



In your contacts.service.ts:



getContacts()
return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users');



In your contacts.component.ts`:



contacts: any = ;
ngOnInit()
this.contactsService.getContacts.subscribe(data => this.contacts = data);


addContact(item)
this.contacts.push(item);



But if you really want to have your contacts list as an Observable, you should use Subject.



In your contacts.service.ts:



$contactsChange = new Subject<any>();
private contactsList = ;
getContacts()
return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users').pipe(map(data =>
this.contactsList = data;
this.$contactsChange.next(this.contactsList);
));


addContact(item)
this.contactsList.push(item);
this.$contactsChange.next(this.contactsList);



In your contacts.component.ts`:



contacts: any = ;
ngOnInit()
this.contactsService.getContacts.subscribe(data => this.contacts = data);
this.contactsService.$contactsChange.subscribe(data => this.contacts = data);






share|improve this answer






















  • I tried this one but it seems not working for me. There is no error but i cant see the new added item
    – Francis Rubia
    Nov 12 at 11:11










  • @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
    – vmoh_ir
    Nov 12 at 11:18










  • i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
    – Francis Rubia
    Nov 12 at 11:22










  • @FrancisRubia I've updated my answer. Check it please.
    – vmoh_ir
    Nov 12 at 11:31










  • Yes, this is actually working but for some reason. I needed to use a service for adding a contact
    – Francis Rubia
    Nov 12 at 11:33


















0














The addContact () method is where you subscribe to the observable getContacts():



getContacts(): Observable<any> 
return this.contact =
this.http.get('https://jsonplaceholder.typicode.com/users');



At the time of subscription is when the call is triggered:



addContact()
let cont: Observable<contactModel>;
cont = this.getContacts();
prueba.finally(() =>
console.log('Finally callback')
)
cont.subscribe(res =>
console.log('at the time of subscription is when the call is triggered')
let resp: contactModel;
resp = res.json() as contactModel;
);








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%2f53260269%2fangular-6-add-items-into-observable%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









    1














    If this.contacts is an Observable of list of objects (contacts: Observable<Items>) and you want to make some changes to that list, you can simply use map like this:



    import map from 'rxjs/operators';

    this.contacts.pipe(map(usersList =>
    usersList.push(newItem);
    return usersList;
    ));


    But if you want to make another request to the server and merge this lists, you can use forkJoin like this:



    import forkJoin from 'rxjs';

    forkJoin(
    this.contacts,
    this.http.get('https://jsonplaceholder.typicode.com/other_users');
    ).pipe(
    map(data =>
    const currentResult = data[0];
    const pastResult = data[1];
    // ...

    ));


    Update



    Based on the your comment for more details, you don't need to do anything with observables. You need is something like this:



    In your contacts.service.ts:



    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users');



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);


    addContact(item)
    this.contacts.push(item);



    But if you really want to have your contacts list as an Observable, you should use Subject.



    In your contacts.service.ts:



    $contactsChange = new Subject<any>();
    private contactsList = ;
    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users').pipe(map(data =>
    this.contactsList = data;
    this.$contactsChange.next(this.contactsList);
    ));


    addContact(item)
    this.contactsList.push(item);
    this.$contactsChange.next(this.contactsList);



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);
    this.contactsService.$contactsChange.subscribe(data => this.contacts = data);






    share|improve this answer






















    • I tried this one but it seems not working for me. There is no error but i cant see the new added item
      – Francis Rubia
      Nov 12 at 11:11










    • @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
      – vmoh_ir
      Nov 12 at 11:18










    • i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
      – Francis Rubia
      Nov 12 at 11:22










    • @FrancisRubia I've updated my answer. Check it please.
      – vmoh_ir
      Nov 12 at 11:31










    • Yes, this is actually working but for some reason. I needed to use a service for adding a contact
      – Francis Rubia
      Nov 12 at 11:33















    1














    If this.contacts is an Observable of list of objects (contacts: Observable<Items>) and you want to make some changes to that list, you can simply use map like this:



    import map from 'rxjs/operators';

    this.contacts.pipe(map(usersList =>
    usersList.push(newItem);
    return usersList;
    ));


    But if you want to make another request to the server and merge this lists, you can use forkJoin like this:



    import forkJoin from 'rxjs';

    forkJoin(
    this.contacts,
    this.http.get('https://jsonplaceholder.typicode.com/other_users');
    ).pipe(
    map(data =>
    const currentResult = data[0];
    const pastResult = data[1];
    // ...

    ));


    Update



    Based on the your comment for more details, you don't need to do anything with observables. You need is something like this:



    In your contacts.service.ts:



    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users');



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);


    addContact(item)
    this.contacts.push(item);



    But if you really want to have your contacts list as an Observable, you should use Subject.



    In your contacts.service.ts:



    $contactsChange = new Subject<any>();
    private contactsList = ;
    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users').pipe(map(data =>
    this.contactsList = data;
    this.$contactsChange.next(this.contactsList);
    ));


    addContact(item)
    this.contactsList.push(item);
    this.$contactsChange.next(this.contactsList);



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);
    this.contactsService.$contactsChange.subscribe(data => this.contacts = data);






    share|improve this answer






















    • I tried this one but it seems not working for me. There is no error but i cant see the new added item
      – Francis Rubia
      Nov 12 at 11:11










    • @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
      – vmoh_ir
      Nov 12 at 11:18










    • i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
      – Francis Rubia
      Nov 12 at 11:22










    • @FrancisRubia I've updated my answer. Check it please.
      – vmoh_ir
      Nov 12 at 11:31










    • Yes, this is actually working but for some reason. I needed to use a service for adding a contact
      – Francis Rubia
      Nov 12 at 11:33













    1












    1








    1






    If this.contacts is an Observable of list of objects (contacts: Observable<Items>) and you want to make some changes to that list, you can simply use map like this:



    import map from 'rxjs/operators';

    this.contacts.pipe(map(usersList =>
    usersList.push(newItem);
    return usersList;
    ));


    But if you want to make another request to the server and merge this lists, you can use forkJoin like this:



    import forkJoin from 'rxjs';

    forkJoin(
    this.contacts,
    this.http.get('https://jsonplaceholder.typicode.com/other_users');
    ).pipe(
    map(data =>
    const currentResult = data[0];
    const pastResult = data[1];
    // ...

    ));


    Update



    Based on the your comment for more details, you don't need to do anything with observables. You need is something like this:



    In your contacts.service.ts:



    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users');



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);


    addContact(item)
    this.contacts.push(item);



    But if you really want to have your contacts list as an Observable, you should use Subject.



    In your contacts.service.ts:



    $contactsChange = new Subject<any>();
    private contactsList = ;
    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users').pipe(map(data =>
    this.contactsList = data;
    this.$contactsChange.next(this.contactsList);
    ));


    addContact(item)
    this.contactsList.push(item);
    this.$contactsChange.next(this.contactsList);



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);
    this.contactsService.$contactsChange.subscribe(data => this.contacts = data);






    share|improve this answer














    If this.contacts is an Observable of list of objects (contacts: Observable<Items>) and you want to make some changes to that list, you can simply use map like this:



    import map from 'rxjs/operators';

    this.contacts.pipe(map(usersList =>
    usersList.push(newItem);
    return usersList;
    ));


    But if you want to make another request to the server and merge this lists, you can use forkJoin like this:



    import forkJoin from 'rxjs';

    forkJoin(
    this.contacts,
    this.http.get('https://jsonplaceholder.typicode.com/other_users');
    ).pipe(
    map(data =>
    const currentResult = data[0];
    const pastResult = data[1];
    // ...

    ));


    Update



    Based on the your comment for more details, you don't need to do anything with observables. You need is something like this:



    In your contacts.service.ts:



    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users');



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);


    addContact(item)
    this.contacts.push(item);



    But if you really want to have your contacts list as an Observable, you should use Subject.



    In your contacts.service.ts:



    $contactsChange = new Subject<any>();
    private contactsList = ;
    getContacts()
    return this.contact = this.http.get('https://jsonplaceholder.typicode.com/users').pipe(map(data =>
    this.contactsList = data;
    this.$contactsChange.next(this.contactsList);
    ));


    addContact(item)
    this.contactsList.push(item);
    this.$contactsChange.next(this.contactsList);



    In your contacts.component.ts`:



    contacts: any = ;
    ngOnInit()
    this.contactsService.getContacts.subscribe(data => this.contacts = data);
    this.contactsService.$contactsChange.subscribe(data => this.contacts = data);







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 11:53

























    answered Nov 12 at 10:45









    vmoh_ir

    1,3161424




    1,3161424











    • I tried this one but it seems not working for me. There is no error but i cant see the new added item
      – Francis Rubia
      Nov 12 at 11:11










    • @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
      – vmoh_ir
      Nov 12 at 11:18










    • i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
      – Francis Rubia
      Nov 12 at 11:22










    • @FrancisRubia I've updated my answer. Check it please.
      – vmoh_ir
      Nov 12 at 11:31










    • Yes, this is actually working but for some reason. I needed to use a service for adding a contact
      – Francis Rubia
      Nov 12 at 11:33
















    • I tried this one but it seems not working for me. There is no error but i cant see the new added item
      – Francis Rubia
      Nov 12 at 11:11










    • @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
      – vmoh_ir
      Nov 12 at 11:18










    • i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
      – Francis Rubia
      Nov 12 at 11:22










    • @FrancisRubia I've updated my answer. Check it please.
      – vmoh_ir
      Nov 12 at 11:31










    • Yes, this is actually working but for some reason. I needed to use a service for adding a contact
      – Francis Rubia
      Nov 12 at 11:33















    I tried this one but it seems not working for me. There is no error but i cant see the new added item
    – Francis Rubia
    Nov 12 at 11:11




    I tried this one but it seems not working for me. There is no error but i cant see the new added item
    – Francis Rubia
    Nov 12 at 11:11












    @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
    – vmoh_ir
    Nov 12 at 11:18




    @FrancisRubia When are you subscribing to this.contacts? If you do that before calling addContact, then you won't see any changes.
    – vmoh_ir
    Nov 12 at 11:18












    i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
    – Francis Rubia
    Nov 12 at 11:22




    i'm subscribing on init. After the page load i should see those default user from jsonplaceholder.com and after adding a contact i should be able to see the changes from the list. How am i supposed to do it right
    – Francis Rubia
    Nov 12 at 11:22












    @FrancisRubia I've updated my answer. Check it please.
    – vmoh_ir
    Nov 12 at 11:31




    @FrancisRubia I've updated my answer. Check it please.
    – vmoh_ir
    Nov 12 at 11:31












    Yes, this is actually working but for some reason. I needed to use a service for adding a contact
    – Francis Rubia
    Nov 12 at 11:33




    Yes, this is actually working but for some reason. I needed to use a service for adding a contact
    – Francis Rubia
    Nov 12 at 11:33













    0














    The addContact () method is where you subscribe to the observable getContacts():



    getContacts(): Observable<any> 
    return this.contact =
    this.http.get('https://jsonplaceholder.typicode.com/users');



    At the time of subscription is when the call is triggered:



    addContact()
    let cont: Observable<contactModel>;
    cont = this.getContacts();
    prueba.finally(() =>
    console.log('Finally callback')
    )
    cont.subscribe(res =>
    console.log('at the time of subscription is when the call is triggered')
    let resp: contactModel;
    resp = res.json() as contactModel;
    );








    share|improve this answer

























      0














      The addContact () method is where you subscribe to the observable getContacts():



      getContacts(): Observable<any> 
      return this.contact =
      this.http.get('https://jsonplaceholder.typicode.com/users');



      At the time of subscription is when the call is triggered:



      addContact()
      let cont: Observable<contactModel>;
      cont = this.getContacts();
      prueba.finally(() =>
      console.log('Finally callback')
      )
      cont.subscribe(res =>
      console.log('at the time of subscription is when the call is triggered')
      let resp: contactModel;
      resp = res.json() as contactModel;
      );








      share|improve this answer























        0












        0








        0






        The addContact () method is where you subscribe to the observable getContacts():



        getContacts(): Observable<any> 
        return this.contact =
        this.http.get('https://jsonplaceholder.typicode.com/users');



        At the time of subscription is when the call is triggered:



        addContact()
        let cont: Observable<contactModel>;
        cont = this.getContacts();
        prueba.finally(() =>
        console.log('Finally callback')
        )
        cont.subscribe(res =>
        console.log('at the time of subscription is when the call is triggered')
        let resp: contactModel;
        resp = res.json() as contactModel;
        );








        share|improve this answer












        The addContact () method is where you subscribe to the observable getContacts():



        getContacts(): Observable<any> 
        return this.contact =
        this.http.get('https://jsonplaceholder.typicode.com/users');



        At the time of subscription is when the call is triggered:



        addContact()
        let cont: Observable<contactModel>;
        cont = this.getContacts();
        prueba.finally(() =>
        console.log('Finally callback')
        )
        cont.subscribe(res =>
        console.log('at the time of subscription is when the call is triggered')
        let resp: contactModel;
        resp = res.json() as contactModel;
        );









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 11:23









        ararb78

        1331319




        1331319



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53260269%2fangular-6-add-items-into-observable%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党