XIANFESchool / FE-problem-collection

前端问题收集和知识经验总结
https://github.com/ShuyunXIANFESchool/FE-problem-collection/issues
63 stars 22 forks source link

关于ESlint校验 angular/on-watch 如何fix问题 #19

Open hjzheng opened 8 years ago

hjzheng commented 8 years ago

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

(function() {
    angular.module('app').run(run);

    run.$inject = ['$rootScope', '$location', '$http'];

    function run($rootScope, $location, $http) {
        var callback = $rootScope.$watch(function() {
            return $location.search();
        }, function(newValue) {
            if (newValue.token) {
                $http.defaults.headers.common.token = newValue.token;
            }
        });

        $rootScope.$on('$destroy', callback);
    }
})();