Skip to content

angular/on-watch error #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tossp opened this issue Oct 12, 2015 · 11 comments
Closed

angular/on-watch error #231

tossp opened this issue Oct 12, 2015 · 11 comments
Labels

Comments

@tossp
Copy link

tossp commented Oct 12, 2015

hi,guys
How do I fix this error?

Error Messages:

  131:7  error  The "$on" call should be assigned to a variable, in order to be destroyed during the $destroy event  angular/on-watch
  140:7  error  The "$on" call should be assigned to a variable, in order to be destroyed during the $destroy event  angular/on-watch
  145:7  error  The "$on" call should be assigned to a variable, in order to be destroyed during the $destroy event  angular/on-watch
  150:7  error  The "$on" call should be assigned to a variable, in order to be destroyed during the $destroy event  angular/on-watch

Corresponding Source:

      $rootScope.$on('$stateChangeStart', function (event, toState) {
        //$log.debug(1, '$stateChangeStart', event, toState, toParams, fromState, fromParams);

        handleHtmlId(toState);
        detachOnResize();
      });

      // initialized with 1 cause we came here with one $viewContentLoading request
      var viewContentLoading = 1;
      $rootScope.$on('$viewContentLoading', function () {
        //$log.debug(2, '$viewContentLoading', event, viewConfig);
        viewContentLoading++;
      });

      $rootScope.$on('$stateChangeSuccess', function () {
        //$log.debug(3, '$stateChangeSuccess', event, toState, toParams, fromState, fromParams);
        forceResizeTrigger = true;
      });

      $rootScope.$on('$viewContentLoaded', function () {
        //$log.debug(4, '$viewContentLoaded', event);
        viewContentLoading--;

        if (viewContentLoading === 0 && initialized) {
          debouncedRun();
        }
      });
@tilmanschweitzer
Copy link
Collaborator

Documentation: https://docs.angularjs.org/api/ng/type/$rootScope.Scope - Returns a deregistration function for this listener.

var deregisterationCallback = $rootScope.$on(....
$rootScope.$on('$destroy', deregistrationCallback)

http://stackoverflow.com/questions/25113884/should-angular-watch-be-removed-when-scope-destroyed

@tossp
Copy link
Author

tossp commented Oct 12, 2015

@tilmanpotthof Thank you very much!

@gitnik
Copy link

gitnik commented Nov 4, 2015

Does this really make sense? Shouldn't this lint ignore the $rootScope? Destroying the $rootScope implies the app being torn down, which makes unsubscribing kinda useless

@gitnik
Copy link

gitnik commented Nov 11, 2015

I would love to submit a PR if this behaviour is not wanted

@tilmanschweitzer
Copy link
Collaborator

@gitnik Good point. I think unsubscribing from the $rootScope does not make sense, at least with the $destroy event.

... or did I miss something? @remcohaszing @Gillespie59

@remcohaszing
Copy link
Collaborator

Seems legit.

@gitnik
Copy link

gitnik commented Nov 11, 2015

OK I will submit a PR then :-)

@gitnik
Copy link

gitnik commented Nov 11, 2015

Shouldn't this actually only lint on? Removing $watch is not needed, as the previously SO answer already explains

@laurent-le-graverend
Copy link

👍

@gitnik
Copy link

gitnik commented Nov 13, 2015

#264

@charneykaye
Copy link

Any reason not to do this?

$rootScope.$on('$destroy', $rootScope.$on(EVENTS.changeRegion, function() {
  vm.nowRegion = OpsService.getRegion();
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants