xjpro / react-in-angularjs

A super simple way to render React components in AngularJS
The Unlicense
23 stars 13 forks source link

Scope not injected when production optimizations are enabled #16

Closed tuhlmann closed 1 year ago

tuhlmann commented 2 years ago

Hey,

we discovered in our project that AngularJS will not inject the $scope into the React component if AngularJS's production optimizations are enabled.

These optimizations are described here: https://docs.angularjs.org/guide/production

The problem is, that the call to .scope() of an angular component will return null if debugging is disabled. It appears that this call to access the scope of a component was meant as a debugging aid- without making this obvious by the Angular designers.

This is the offending line:

this.$scope = window.angular.element($element).scope();

I'm not sure if there would be a workaround for this issue. Great if you know any, but we think we can live with this until all of the app is migrated.

Just thought to pass the info along, maybe you want to add this caveat to the Readme.

Thanks, Torsten.

ornic commented 2 years ago

Can you provide an example when the $scope of angular is needed inside react compontent? We're using v18 fork of this great project now for migration rather large angularjs app to the "shiny new world" of react and in all cases attribs is enough.

xjpro commented 2 years ago

I've used $scope inside React before, primarily to do $scope.$apply and get mutations communicated back to AngularJS. I would assert this is an anti-pattern to be avoided but in some rare cases it was a lot more work to do it in a better way.

@tuhlmann Appreciate the heads up, I will update the README. I'm not sure if there's too much point in putting effort to fixing this but it's good to know.

tuhlmann commented 2 years ago

@ornic We try to not use the $scope in React componente, but for interaction that's sometimes needed. We used it to emit signals on or to catch events sent from an Angular component and these kinds of things.

@xjpro Don't worry too much about this problem. We mitigated by switching off production optimization, and I would assume that most people haven't heard of it before (I hadn't). It doesn't impact us right now.