Strange subscribe behaviour





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







0















I'm working with Angular 6 and I've got a strange subscribe behavior, so when I call reload method for three times from the constructor it works fine and I got three log message "subscribe".



But when I click on a button which have (click)="reload()", the reload is executed but the subscribe is never executed.



company-edit.component.ts



export class CompanyEditComponent implements OnInit {
company$ : Observable<any>;
company: Company;
formCompany: Company;
refDb: any;

constructor(private db: AngularFireDatabase, private companyService: CompanyService) {
this.refDb = db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.reload();
this.reload();
this.reload();
}

reload(){
console.log("reload");
this.refDb = this.db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.formCompany = new Company("","");
this.company$.subscribe( (company)=>{ console.log("subscribe");

this.company = company;
console.log(this.company);

if (company)
this.formCompany = company;
});
// this.formCompany = {"name":"kk","fondator":"gg"};
}


company-edit.component.html



<div>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.name" placeholder="Name">
</mat-form-field>
<br>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.fondator" placeholder="Fondateur">
</mat-form-field>
<br>
<button mat-raised-button color="primary"(click)="submit()">Save</button> |
<button mat-raised-button color="accent"(click)="update()">Update</button> |
<button mat-raised-button color="warn"(click)="delete()">Delete</button> |
<button mat-raised-button color="" (click)="reload()">Reload</button>
</div>


company.service.ts



export class CompanyService {
company$ : Observable<any>;
refDb: any;

constructor(private db: AngularFireDatabase) {
this.refDb = db.object('company');
this.company$ = this.refDb.valueChanges();
}

getCompanyObservable(){
return this.company$;
}









share|improve this question


















  • 1





    Just a side note, but if you have these buttons inside a form you should specify the button type, otherwise they will per default be submit buttons instead of type="button". I don't know if events are fired in your case.

    – Silvermind
    Nov 16 '18 at 16:00











  • Yes I should mention the type of the button inside a form, but for this exemple when I click the button "reload", the function reload() is correctly called but on the execution the sucscribe is ignored.

    – Karim Garali
    Nov 16 '18 at 16:04






  • 1





    Why would you resubscribe to company$ in the first place? It kind of defeats the purpose. My guess is the reason you're seeing the 3 logs on load, is because the subscriptions are in place before the Observable emits data. If nothing's changed, it will not emit data, which would be the reason you're not seeing a 'subscribe' log.

    – Tim VN
    Nov 16 '18 at 16:11


















0















I'm working with Angular 6 and I've got a strange subscribe behavior, so when I call reload method for three times from the constructor it works fine and I got three log message "subscribe".



But when I click on a button which have (click)="reload()", the reload is executed but the subscribe is never executed.



company-edit.component.ts



export class CompanyEditComponent implements OnInit {
company$ : Observable<any>;
company: Company;
formCompany: Company;
refDb: any;

constructor(private db: AngularFireDatabase, private companyService: CompanyService) {
this.refDb = db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.reload();
this.reload();
this.reload();
}

reload(){
console.log("reload");
this.refDb = this.db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.formCompany = new Company("","");
this.company$.subscribe( (company)=>{ console.log("subscribe");

this.company = company;
console.log(this.company);

if (company)
this.formCompany = company;
});
// this.formCompany = {"name":"kk","fondator":"gg"};
}


company-edit.component.html



<div>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.name" placeholder="Name">
</mat-form-field>
<br>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.fondator" placeholder="Fondateur">
</mat-form-field>
<br>
<button mat-raised-button color="primary"(click)="submit()">Save</button> |
<button mat-raised-button color="accent"(click)="update()">Update</button> |
<button mat-raised-button color="warn"(click)="delete()">Delete</button> |
<button mat-raised-button color="" (click)="reload()">Reload</button>
</div>


company.service.ts



export class CompanyService {
company$ : Observable<any>;
refDb: any;

constructor(private db: AngularFireDatabase) {
this.refDb = db.object('company');
this.company$ = this.refDb.valueChanges();
}

getCompanyObservable(){
return this.company$;
}









share|improve this question


















  • 1





    Just a side note, but if you have these buttons inside a form you should specify the button type, otherwise they will per default be submit buttons instead of type="button". I don't know if events are fired in your case.

    – Silvermind
    Nov 16 '18 at 16:00











  • Yes I should mention the type of the button inside a form, but for this exemple when I click the button "reload", the function reload() is correctly called but on the execution the sucscribe is ignored.

    – Karim Garali
    Nov 16 '18 at 16:04






  • 1





    Why would you resubscribe to company$ in the first place? It kind of defeats the purpose. My guess is the reason you're seeing the 3 logs on load, is because the subscriptions are in place before the Observable emits data. If nothing's changed, it will not emit data, which would be the reason you're not seeing a 'subscribe' log.

    – Tim VN
    Nov 16 '18 at 16:11














0












0








0








I'm working with Angular 6 and I've got a strange subscribe behavior, so when I call reload method for three times from the constructor it works fine and I got three log message "subscribe".



But when I click on a button which have (click)="reload()", the reload is executed but the subscribe is never executed.



company-edit.component.ts



export class CompanyEditComponent implements OnInit {
company$ : Observable<any>;
company: Company;
formCompany: Company;
refDb: any;

constructor(private db: AngularFireDatabase, private companyService: CompanyService) {
this.refDb = db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.reload();
this.reload();
this.reload();
}

reload(){
console.log("reload");
this.refDb = this.db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.formCompany = new Company("","");
this.company$.subscribe( (company)=>{ console.log("subscribe");

this.company = company;
console.log(this.company);

if (company)
this.formCompany = company;
});
// this.formCompany = {"name":"kk","fondator":"gg"};
}


company-edit.component.html



<div>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.name" placeholder="Name">
</mat-form-field>
<br>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.fondator" placeholder="Fondateur">
</mat-form-field>
<br>
<button mat-raised-button color="primary"(click)="submit()">Save</button> |
<button mat-raised-button color="accent"(click)="update()">Update</button> |
<button mat-raised-button color="warn"(click)="delete()">Delete</button> |
<button mat-raised-button color="" (click)="reload()">Reload</button>
</div>


company.service.ts



export class CompanyService {
company$ : Observable<any>;
refDb: any;

constructor(private db: AngularFireDatabase) {
this.refDb = db.object('company');
this.company$ = this.refDb.valueChanges();
}

getCompanyObservable(){
return this.company$;
}









share|improve this question














I'm working with Angular 6 and I've got a strange subscribe behavior, so when I call reload method for three times from the constructor it works fine and I got three log message "subscribe".



But when I click on a button which have (click)="reload()", the reload is executed but the subscribe is never executed.



company-edit.component.ts



export class CompanyEditComponent implements OnInit {
company$ : Observable<any>;
company: Company;
formCompany: Company;
refDb: any;

constructor(private db: AngularFireDatabase, private companyService: CompanyService) {
this.refDb = db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.reload();
this.reload();
this.reload();
}

reload(){
console.log("reload");
this.refDb = this.db.object('company');
this.company$ = this.companyService.getCompanyObservable();
this.formCompany = new Company("","");
this.company$.subscribe( (company)=>{ console.log("subscribe");

this.company = company;
console.log(this.company);

if (company)
this.formCompany = company;
});
// this.formCompany = {"name":"kk","fondator":"gg"};
}


company-edit.component.html



<div>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.name" placeholder="Name">
</mat-form-field>
<br>
<mat-form-field>
<input matInput [(ngModel)]="formCompany.fondator" placeholder="Fondateur">
</mat-form-field>
<br>
<button mat-raised-button color="primary"(click)="submit()">Save</button> |
<button mat-raised-button color="accent"(click)="update()">Update</button> |
<button mat-raised-button color="warn"(click)="delete()">Delete</button> |
<button mat-raised-button color="" (click)="reload()">Reload</button>
</div>


company.service.ts



export class CompanyService {
company$ : Observable<any>;
refDb: any;

constructor(private db: AngularFireDatabase) {
this.refDb = db.object('company');
this.company$ = this.refDb.valueChanges();
}

getCompanyObservable(){
return this.company$;
}






angular observable angularfire2 angular7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 15:53









Karim GaraliKarim Garali

358




358








  • 1





    Just a side note, but if you have these buttons inside a form you should specify the button type, otherwise they will per default be submit buttons instead of type="button". I don't know if events are fired in your case.

    – Silvermind
    Nov 16 '18 at 16:00











  • Yes I should mention the type of the button inside a form, but for this exemple when I click the button "reload", the function reload() is correctly called but on the execution the sucscribe is ignored.

    – Karim Garali
    Nov 16 '18 at 16:04






  • 1





    Why would you resubscribe to company$ in the first place? It kind of defeats the purpose. My guess is the reason you're seeing the 3 logs on load, is because the subscriptions are in place before the Observable emits data. If nothing's changed, it will not emit data, which would be the reason you're not seeing a 'subscribe' log.

    – Tim VN
    Nov 16 '18 at 16:11














  • 1





    Just a side note, but if you have these buttons inside a form you should specify the button type, otherwise they will per default be submit buttons instead of type="button". I don't know if events are fired in your case.

    – Silvermind
    Nov 16 '18 at 16:00











  • Yes I should mention the type of the button inside a form, but for this exemple when I click the button "reload", the function reload() is correctly called but on the execution the sucscribe is ignored.

    – Karim Garali
    Nov 16 '18 at 16:04






  • 1





    Why would you resubscribe to company$ in the first place? It kind of defeats the purpose. My guess is the reason you're seeing the 3 logs on load, is because the subscriptions are in place before the Observable emits data. If nothing's changed, it will not emit data, which would be the reason you're not seeing a 'subscribe' log.

    – Tim VN
    Nov 16 '18 at 16:11








1




1





Just a side note, but if you have these buttons inside a form you should specify the button type, otherwise they will per default be submit buttons instead of type="button". I don't know if events are fired in your case.

– Silvermind
Nov 16 '18 at 16:00





Just a side note, but if you have these buttons inside a form you should specify the button type, otherwise they will per default be submit buttons instead of type="button". I don't know if events are fired in your case.

– Silvermind
Nov 16 '18 at 16:00













Yes I should mention the type of the button inside a form, but for this exemple when I click the button "reload", the function reload() is correctly called but on the execution the sucscribe is ignored.

– Karim Garali
Nov 16 '18 at 16:04





Yes I should mention the type of the button inside a form, but for this exemple when I click the button "reload", the function reload() is correctly called but on the execution the sucscribe is ignored.

– Karim Garali
Nov 16 '18 at 16:04




1




1





Why would you resubscribe to company$ in the first place? It kind of defeats the purpose. My guess is the reason you're seeing the 3 logs on load, is because the subscriptions are in place before the Observable emits data. If nothing's changed, it will not emit data, which would be the reason you're not seeing a 'subscribe' log.

– Tim VN
Nov 16 '18 at 16:11





Why would you resubscribe to company$ in the first place? It kind of defeats the purpose. My guess is the reason you're seeing the 3 logs on load, is because the subscriptions are in place before the Observable emits data. If nothing's changed, it will not emit data, which would be the reason you're not seeing a 'subscribe' log.

– Tim VN
Nov 16 '18 at 16:11












1 Answer
1






active

oldest

votes


















0














It looks like you need the latest value received from the server. You can achieve the same by using BehaviorSubject



export class CompanyService {
//company$ : Observable<any>;

public data: BehaviorSubject<any> = new BehaviorSubject(null);

refDb: any;

constructor(private db: AngularFireDatabase) {
this.refDb = db.object('company');
//this.company$ = this.refDb.valueChanges();
this.refDb.valueChanges().subscribe(data=>this.data.next(data));
}

getCompanyObservable(){
return this.data.asObservable();
}





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%2f53341305%2fstrange-subscribe-behaviour%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It looks like you need the latest value received from the server. You can achieve the same by using BehaviorSubject



    export class CompanyService {
    //company$ : Observable<any>;

    public data: BehaviorSubject<any> = new BehaviorSubject(null);

    refDb: any;

    constructor(private db: AngularFireDatabase) {
    this.refDb = db.object('company');
    //this.company$ = this.refDb.valueChanges();
    this.refDb.valueChanges().subscribe(data=>this.data.next(data));
    }

    getCompanyObservable(){
    return this.data.asObservable();
    }





    share|improve this answer






























      0














      It looks like you need the latest value received from the server. You can achieve the same by using BehaviorSubject



      export class CompanyService {
      //company$ : Observable<any>;

      public data: BehaviorSubject<any> = new BehaviorSubject(null);

      refDb: any;

      constructor(private db: AngularFireDatabase) {
      this.refDb = db.object('company');
      //this.company$ = this.refDb.valueChanges();
      this.refDb.valueChanges().subscribe(data=>this.data.next(data));
      }

      getCompanyObservable(){
      return this.data.asObservable();
      }





      share|improve this answer




























        0












        0








        0







        It looks like you need the latest value received from the server. You can achieve the same by using BehaviorSubject



        export class CompanyService {
        //company$ : Observable<any>;

        public data: BehaviorSubject<any> = new BehaviorSubject(null);

        refDb: any;

        constructor(private db: AngularFireDatabase) {
        this.refDb = db.object('company');
        //this.company$ = this.refDb.valueChanges();
        this.refDb.valueChanges().subscribe(data=>this.data.next(data));
        }

        getCompanyObservable(){
        return this.data.asObservable();
        }





        share|improve this answer















        It looks like you need the latest value received from the server. You can achieve the same by using BehaviorSubject



        export class CompanyService {
        //company$ : Observable<any>;

        public data: BehaviorSubject<any> = new BehaviorSubject(null);

        refDb: any;

        constructor(private db: AngularFireDatabase) {
        this.refDb = db.object('company');
        //this.company$ = this.refDb.valueChanges();
        this.refDb.valueChanges().subscribe(data=>this.data.next(data));
        }

        getCompanyObservable(){
        return this.data.asObservable();
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 21:40

























        answered Nov 16 '18 at 21:34









        Sunil SinghSunil Singh

        6,4872628




        6,4872628
































            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%2f53341305%2fstrange-subscribe-behaviour%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

            Bressuire

            Vorschmack

            Quarantine