我正在尝试创建一个在后台运行的服务,并监视 URL 更改并将新参数发送到各个 Controller 。
我知道我需要在 app.run 中启动此服务,以便它从一开始就启动。 我没有使用 ng-router 提供程序,因此我没有使用 $locationchangesuccess 或 $stateChangeStart。 此外,我不能使用 $scope.$watch 因为这是一项服务。
有什么想法吗?
最佳答案
也许做一个$watch
:
angular.module('myModule', []).run(myUrlWatcher);
function myUrlWatcher($rootScope, $location) {
'ngInject';
$rootScope.$watch(function () {
return $location.url();
}, function () {
/// ....
console.log('in-app url changed, do wahtever you want to do here')
// ....
})
}
关于javascript - AngularJS 中的后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51356026/