Angular wont detect change for reference object when using injected service's aray for angular-calendarHow to detect a route change in Angular?Triggering change detection manually in AngularHow to detect when an @Input() value changes in Angular?Change detection on injected service's property in angular2Angular change detection on @Input objectAngular Service's Dependencies are not injected when service function is used as Input parameterDetect change of the property of Input() object in Angular 2+Callback angular function from jquery eventAngular: Change in Pipe not detectedUI component not updating accordingly to data change

Non-trope happy ending?

Limits and Infinite Integration by Parts

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Did arcade monitors have same pixel aspect ratio as TV sets?

Add big quotation marks inside my colorbox

Creepy dinosaur pc game identification

Why "had" in "[something] we would have made had we used [something]"?

Why can Carol Danvers change her suit colours in the first place?

How do you make your own symbol when Detexify fails?

Why is so much work done on numerical verification of the Riemann Hypothesis?

How much character growth crosses the line into breaking the character

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

PTIJ: Haman's bad computer

Hero deduces identity of a killer

Why does a simple loop result in ASYNC_NETWORK_IO waits?

Pre-mixing cryogenic fuels and using only one fuel tank

What happens to a creature that changes size inside of Otiluke's Resilient Sphere?

Do the primes contain an infinite almost arithmetic progression?

What should you do when eye contact makes your subordinate uncomfortable?

Using substitution ciphers to generate new alphabets in a novel

What should you do if you miss a job interview (deliberately)?

Temporarily disable WLAN internet access for children, but allow it for adults

How can "mimic phobia" be cured or prevented?

How could a planet have erratic days?



Angular wont detect change for reference object when using injected service's aray for angular-calendar


How to detect a route change in Angular?Triggering change detection manually in AngularHow to detect when an @Input() value changes in Angular?Change detection on injected service's property in angular2Angular change detection on @Input objectAngular Service's Dependencies are not injected when service function is used as Input parameterDetect change of the property of Input() object in Angular 2+Callback angular function from jquery eventAngular: Change in Pipe not detectedUI component not updating accordingly to data change













1















So I am using angular-calendar and I have a dialog for when I press a button, right now I add a random event object to my calendarservice events array for testing purposes:



 onButtonClick() 
var date = addHours(new Date(), 2)
this.calendarService.onCreateEvent('test added',date )
console.log(date)



which adds it to my injected service:



export class CalendarService {
events: CalendarEvent[] = [
title: "my test event 1", start: new Date() ,
title: "my test event 2", start: addDays(new Date(), 1)
];


Right now I use ngOnit to set the local events array in the component:



export class ScheduleCalendarComponent implements OnInit{
constructor(
private calendarService: CalendarService,
public dialog: MatDialog
)

view: string = "month";

viewDate: Date = new Date();

events: CalendarEvent[] = [];

refresh: Subject<any> = new Subject();

ngOnInit(): void
this.events = this.calendarService.events;



So right now it pulls the correct events but when I add an event it doesnt update the DOM because I only update the array OnInit so only after changing pages and coming back, Using the calendarservice events array it works if I change the update method:



 onCreateEvent(title: string, start: any) 
this.events = [...this.events, title: title, start: start]



I use this and add it to the calendar like so:



 <mwl-calendar-day-view
*ngSwitchCase="'day'"
[viewDate]="viewDate"
[events]="this.calendarService.events"
(hourSegmentClicked)="openDialog()"
>
</mwl-calendar-day-view>


but how would I do this using the components own array rather than the service array? And is it better practice for the component to use its own array like max did than just take straight from the service?



I'd like to do it using the components array but It wont update on button click:



 <mwl-calendar-day-view
*ngSwitchCase="'day'"
[viewDate]="viewDate"
[events]="events"
(hourSegmentClicked)="openDialog()"
>
</mwl-calendar-day-view>


Hope that makes sense



Thanks you!










share|improve this question


























    1















    So I am using angular-calendar and I have a dialog for when I press a button, right now I add a random event object to my calendarservice events array for testing purposes:



     onButtonClick() 
    var date = addHours(new Date(), 2)
    this.calendarService.onCreateEvent('test added',date )
    console.log(date)



    which adds it to my injected service:



    export class CalendarService {
    events: CalendarEvent[] = [
    title: "my test event 1", start: new Date() ,
    title: "my test event 2", start: addDays(new Date(), 1)
    ];


    Right now I use ngOnit to set the local events array in the component:



    export class ScheduleCalendarComponent implements OnInit{
    constructor(
    private calendarService: CalendarService,
    public dialog: MatDialog
    )

    view: string = "month";

    viewDate: Date = new Date();

    events: CalendarEvent[] = [];

    refresh: Subject<any> = new Subject();

    ngOnInit(): void
    this.events = this.calendarService.events;



    So right now it pulls the correct events but when I add an event it doesnt update the DOM because I only update the array OnInit so only after changing pages and coming back, Using the calendarservice events array it works if I change the update method:



     onCreateEvent(title: string, start: any) 
    this.events = [...this.events, title: title, start: start]



    I use this and add it to the calendar like so:



     <mwl-calendar-day-view
    *ngSwitchCase="'day'"
    [viewDate]="viewDate"
    [events]="this.calendarService.events"
    (hourSegmentClicked)="openDialog()"
    >
    </mwl-calendar-day-view>


    but how would I do this using the components own array rather than the service array? And is it better practice for the component to use its own array like max did than just take straight from the service?



    I'd like to do it using the components array but It wont update on button click:



     <mwl-calendar-day-view
    *ngSwitchCase="'day'"
    [viewDate]="viewDate"
    [events]="events"
    (hourSegmentClicked)="openDialog()"
    >
    </mwl-calendar-day-view>


    Hope that makes sense



    Thanks you!










    share|improve this question
























      1












      1








      1


      1






      So I am using angular-calendar and I have a dialog for when I press a button, right now I add a random event object to my calendarservice events array for testing purposes:



       onButtonClick() 
      var date = addHours(new Date(), 2)
      this.calendarService.onCreateEvent('test added',date )
      console.log(date)



      which adds it to my injected service:



      export class CalendarService {
      events: CalendarEvent[] = [
      title: "my test event 1", start: new Date() ,
      title: "my test event 2", start: addDays(new Date(), 1)
      ];


      Right now I use ngOnit to set the local events array in the component:



      export class ScheduleCalendarComponent implements OnInit{
      constructor(
      private calendarService: CalendarService,
      public dialog: MatDialog
      )

      view: string = "month";

      viewDate: Date = new Date();

      events: CalendarEvent[] = [];

      refresh: Subject<any> = new Subject();

      ngOnInit(): void
      this.events = this.calendarService.events;



      So right now it pulls the correct events but when I add an event it doesnt update the DOM because I only update the array OnInit so only after changing pages and coming back, Using the calendarservice events array it works if I change the update method:



       onCreateEvent(title: string, start: any) 
      this.events = [...this.events, title: title, start: start]



      I use this and add it to the calendar like so:



       <mwl-calendar-day-view
      *ngSwitchCase="'day'"
      [viewDate]="viewDate"
      [events]="this.calendarService.events"
      (hourSegmentClicked)="openDialog()"
      >
      </mwl-calendar-day-view>


      but how would I do this using the components own array rather than the service array? And is it better practice for the component to use its own array like max did than just take straight from the service?



      I'd like to do it using the components array but It wont update on button click:



       <mwl-calendar-day-view
      *ngSwitchCase="'day'"
      [viewDate]="viewDate"
      [events]="events"
      (hourSegmentClicked)="openDialog()"
      >
      </mwl-calendar-day-view>


      Hope that makes sense



      Thanks you!










      share|improve this question














      So I am using angular-calendar and I have a dialog for when I press a button, right now I add a random event object to my calendarservice events array for testing purposes:



       onButtonClick() 
      var date = addHours(new Date(), 2)
      this.calendarService.onCreateEvent('test added',date )
      console.log(date)



      which adds it to my injected service:



      export class CalendarService {
      events: CalendarEvent[] = [
      title: "my test event 1", start: new Date() ,
      title: "my test event 2", start: addDays(new Date(), 1)
      ];


      Right now I use ngOnit to set the local events array in the component:



      export class ScheduleCalendarComponent implements OnInit{
      constructor(
      private calendarService: CalendarService,
      public dialog: MatDialog
      )

      view: string = "month";

      viewDate: Date = new Date();

      events: CalendarEvent[] = [];

      refresh: Subject<any> = new Subject();

      ngOnInit(): void
      this.events = this.calendarService.events;



      So right now it pulls the correct events but when I add an event it doesnt update the DOM because I only update the array OnInit so only after changing pages and coming back, Using the calendarservice events array it works if I change the update method:



       onCreateEvent(title: string, start: any) 
      this.events = [...this.events, title: title, start: start]



      I use this and add it to the calendar like so:



       <mwl-calendar-day-view
      *ngSwitchCase="'day'"
      [viewDate]="viewDate"
      [events]="this.calendarService.events"
      (hourSegmentClicked)="openDialog()"
      >
      </mwl-calendar-day-view>


      but how would I do this using the components own array rather than the service array? And is it better practice for the component to use its own array like max did than just take straight from the service?



      I'd like to do it using the components array but It wont update on button click:



       <mwl-calendar-day-view
      *ngSwitchCase="'day'"
      [viewDate]="viewDate"
      [events]="events"
      (hourSegmentClicked)="openDialog()"
      >
      </mwl-calendar-day-view>


      Hope that makes sense



      Thanks you!







      angular dependency-injection angular6 angular2-changedetection angular-calendar






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 1:46









      Syrus YeungSyrus Yeung

      112




      112






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Its happening because there are different changeDetectionStrategy and the one which you are seeing is changeDetectionStrategy.onPush under @Component



          changeDetection: ChangeDetectionStrategy.OnPush


          Why don't you create a new array copy using



          ngOnInit(): void 
          this.events = [...this.calendarService.events];



          OR



          ngOnInit(): void 
          this.events = JSON.parse(JSON.stringify(this.calendarService.events));



          and then you wont modify service array events and can easily use:



          onCreateEvent(title: string, start: any) 
          this.events = [...this.events, title: title, start: start]






          share|improve this answer























          • Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

            – Syrus Yeung
            Mar 8 at 18:58











          • maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

            – Syrus Yeung
            Mar 8 at 19:07











          • @SyrusYeung did u find any solution

            – Shashank Vivek
            Mar 12 at 4:59










          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%2f55055579%2fangular-wont-detect-change-for-reference-object-when-using-injected-services-ar%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














          Its happening because there are different changeDetectionStrategy and the one which you are seeing is changeDetectionStrategy.onPush under @Component



          changeDetection: ChangeDetectionStrategy.OnPush


          Why don't you create a new array copy using



          ngOnInit(): void 
          this.events = [...this.calendarService.events];



          OR



          ngOnInit(): void 
          this.events = JSON.parse(JSON.stringify(this.calendarService.events));



          and then you wont modify service array events and can easily use:



          onCreateEvent(title: string, start: any) 
          this.events = [...this.events, title: title, start: start]






          share|improve this answer























          • Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

            – Syrus Yeung
            Mar 8 at 18:58











          • maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

            – Syrus Yeung
            Mar 8 at 19:07











          • @SyrusYeung did u find any solution

            – Shashank Vivek
            Mar 12 at 4:59















          0














          Its happening because there are different changeDetectionStrategy and the one which you are seeing is changeDetectionStrategy.onPush under @Component



          changeDetection: ChangeDetectionStrategy.OnPush


          Why don't you create a new array copy using



          ngOnInit(): void 
          this.events = [...this.calendarService.events];



          OR



          ngOnInit(): void 
          this.events = JSON.parse(JSON.stringify(this.calendarService.events));



          and then you wont modify service array events and can easily use:



          onCreateEvent(title: string, start: any) 
          this.events = [...this.events, title: title, start: start]






          share|improve this answer























          • Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

            – Syrus Yeung
            Mar 8 at 18:58











          • maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

            – Syrus Yeung
            Mar 8 at 19:07











          • @SyrusYeung did u find any solution

            – Shashank Vivek
            Mar 12 at 4:59













          0












          0








          0







          Its happening because there are different changeDetectionStrategy and the one which you are seeing is changeDetectionStrategy.onPush under @Component



          changeDetection: ChangeDetectionStrategy.OnPush


          Why don't you create a new array copy using



          ngOnInit(): void 
          this.events = [...this.calendarService.events];



          OR



          ngOnInit(): void 
          this.events = JSON.parse(JSON.stringify(this.calendarService.events));



          and then you wont modify service array events and can easily use:



          onCreateEvent(title: string, start: any) 
          this.events = [...this.events, title: title, start: start]






          share|improve this answer













          Its happening because there are different changeDetectionStrategy and the one which you are seeing is changeDetectionStrategy.onPush under @Component



          changeDetection: ChangeDetectionStrategy.OnPush


          Why don't you create a new array copy using



          ngOnInit(): void 
          this.events = [...this.calendarService.events];



          OR



          ngOnInit(): void 
          this.events = JSON.parse(JSON.stringify(this.calendarService.events));



          and then you wont modify service array events and can easily use:



          onCreateEvent(title: string, start: any) 
          this.events = [...this.events, title: title, start: start]







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 3:36









          Shashank VivekShashank Vivek

          5,07832463




          5,07832463












          • Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

            – Syrus Yeung
            Mar 8 at 18:58











          • maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

            – Syrus Yeung
            Mar 8 at 19:07











          • @SyrusYeung did u find any solution

            – Shashank Vivek
            Mar 12 at 4:59

















          • Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

            – Syrus Yeung
            Mar 8 at 18:58











          • maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

            – Syrus Yeung
            Mar 8 at 19:07











          • @SyrusYeung did u find any solution

            – Shashank Vivek
            Mar 12 at 4:59
















          Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

          – Syrus Yeung
          Mar 8 at 18:58





          Unfortunately, neither of these worked, I also tried changing the change detection strategy for each scenario, and none of them worked. the first solution you offered passed the events for service to component but the second one didn't even load the events that are hard coded in the service.

          – Syrus Yeung
          Mar 8 at 18:58













          maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

          – Syrus Yeung
          Mar 8 at 19:07





          maybe this has something to do with it being in ngOnit? I think I've seen it work thos way, however, but since it's only on the initialization of the page maybe it doesn't see it since its only oninit? but i figured since I was passing a reference object it would see it as being a whole new array whenever the service array is changed. not entirely sure

          – Syrus Yeung
          Mar 8 at 19:07













          @SyrusYeung did u find any solution

          – Shashank Vivek
          Mar 12 at 4:59





          @SyrusYeung did u find any solution

          – Shashank Vivek
          Mar 12 at 4:59



















          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%2f55055579%2fangular-wont-detect-change-for-reference-object-when-using-injected-services-ar%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

          How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229