0

I have this in one partial view:

<my-widget id="widget1" fullscreen="true"></my-widget>

And this in another:

<my-widget id="widget2" fullscreen="false"></my-widget>

The component looks like this:

export class MyWidgetComponent implements OnInit  {

    _this: MyWidgetComponent = this;
    @Input() fullscreen: string;

    get myModel(): MyModel {
        return this.myService.getModel();
    }

  ngOnInit() {
      this.myService.initialize(this.fullscreen);
  }

    constructor(
        private myService: MyService,
      ) {
    
      }
}

And then in my-widget.component.html I have:

<dx-circular-gauge [value]="myModel.someValue">
             ...
</dx-circular-gauge>

Everything works fine when there is once instance shown. once I show the second instance of the angular element, and the user changes some things there, myModel.someValue is changed for both widgets, instead of just the one.

The "fullscreen" property has the correct value, for the one widgets it is true, and for the other it's false. What am I doing wrong?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

1 Answers1

0

I don't see where the fullscreen property is actually used, so I might be wrong, but with the code you provided it looks like both of the "my-widget" components will be provided the same instance of myService causing them to have shared state.

  • I added some code in the question, please check it out again. How do I provide them with different version of myService? – petko_stankoski Mar 25 '21 at 14:03
  • 1
    Angular dependency injection creates a singleton of your service at whatever level it's deveined as a provider. So if you added it to your list of providers for your module, every component in that module will have the same copy of the service injected into them. If you want a component to get a unique instance of the service, you can add a providers array to the @Component tag, in the same way that you would add it to a module. – Andrew Klaudt Mar 25 '21 at 21:43
  • @petko_stankoski any luck finding out the reason for this? Its happening to me too – Anil Feb 28 '23 at 17:20