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

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

          How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page