angular / ngMigration-Forum

109 stars 7 forks source link

Question on removing $rootScope from angularJS application #6

Closed BobDankert closed 5 years ago

BobDankert commented 6 years ago

First off - thanks for the efforts with this forum! I used the ngMigration-Assistant tool to analyze our project, and was recommended to go the path of ngUpgrade along with some tasks we are currently undergoing. It confirmed the path/approach we've been working on for our large angular applications. One of the items listed is to remove $rootScope from our application. For the most part we have done this, but we still use this in a couple services to execute some checks when the route changes by using $rootScope.$on('$routeChangeSuccess'. What would be the recommended way to move away from $rootScope but still be able to have a service execute code when a route changes?

We also have the same issue in that we also use $locationChangeSuccess to update some variables in Google Analytics (perhaps there is a better way to do this now, but it was the recommended approach at the time we wrote it).

Any thoughts/advice on how to handle this would be appreciated.

(I also posted this on the ngMigration-Assistant repo, as it's not clear to me which repo questions should be directed to)

BobDankert commented 6 years ago

Also posted to ngMigration-Assistant repo, here: https://github.com/ellamaolson/ngMigration-Assistant/issues/5

ellamaolson commented 6 years ago

Hi @BobDankert, thank you for bringing this up. If you find specific bugs with ngMigration Assistant, please file them in that repo. If you have a general issue or question surrounding migration, please file them here.

BobDankert commented 6 years ago

Perfect. I'll close the issue on ngMigration-Assistant and keep this one open. It was a bit confusing in the Angular blog to know which repo gets the questions :)

jpzwarte commented 6 years ago

@BobDankert you can subscribe to router events and take action accordingly:

    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map(event => event.url),
      filter(url => url.startsWith('....')),
    ).subscribe(() => do stuff);
BobDankert commented 6 years ago

@jpzwarte Thanks - I meant in AngularJS? The ngMigration assistant tool is saying to eliminate $rootScope prior to using ngUpgrade, but I'm not sure how to get rid of $rootScope prior to using ngUpgrade.

jpzwarte commented 6 years ago

I sometimes still use $rootScope in my angular.js code. I wouldn't immediately focus on that. I don't think getting rid of $rootScope is necessary in order to use ngUpgrade.

joshorvis commented 6 years ago

Getting rid of $rootScope isn't necessary - I'm currently using it quite a lot in a hybrid application.

As far as I'm aware, they're making that recommendation because there isn't an equivalent to $rootScope in Angular, so you'll have to do some rewriting when you convert from 1.x to the current version.

ellamaolson commented 6 years ago

Yes, @joshorvis is correct - the reason we are scanning for $rootScope is that it will require manual restructuring of your code in order to move to Angular. However, it can exist as part of a hybrid application, so we will make a change to the recommendation to reflect this.