其中函数被调用AngularJS每次网址的变化? [英] Which function is called each time url changes in AngularJS?

查看:116
本文介绍了其中函数被调用AngularJS每次网址的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做出,而无需等待从angularjs的previous请求的响应同时发生的所有请求的队列。
我有一个加载功能,显示加载的div我每次更改URL路由时间但其不正常,使队列的arrray该功能。

谁能告诉我哪个函数被调用的时候,每次我更改URL路径angularjs路线?
这里是路线code:

  angular.module('对myApp',['myApp.directives','myApp.services','myApp.filters'])。配置(
    功能($ routeProvider,$ locationProvider,$ httpProvider){
        $ locationProvider.html5Mode(假);
        $ locationProvider.hash preFIX(!);        $ httpProvider.defaults.headers.common ['X-要求-随着'] ='XMLHtt prequest';        VAR负荷=功能(数据,headersGetter){
            $('loadingDiv')显示()。
            返回的数据;
        };
        $ httpProvider.defaults.transformRequest.push(装载);        $ routeProvider。
        什么时候('/', {
            templateUrl:'元素的/ home / home.html做为',
            控制器:homeCtrl
        });
   });


解决方案

您可以使用类似:

  .RUN(函数($ rootScope,$位置){
   $ rootScope。$表(函数(){
      返回$ location.path();
    },
    功能(一){
      的console.log('URL已经改变:'+一);
      //显示加载分区,等...
    });
});

I have to make a queue of all requests simultaneously happening without waiting for the response from the previous request in angularjs.
I have a loading function which shows the loading div each time I change the url route but its not ok to make an arrray of queue in that function.

Can anyone tell me which function is called in angularjs routes when each time I change the url route ?
HEre is the routes code :

angular.module('myApp', ['myApp.directives', 'myApp.services', 'myApp.filters']).config(
    function($routeProvider, $locationProvider, $httpProvider) {
        $locationProvider.html5Mode(false);
        $locationProvider.hashPrefix('!');

        $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

        var loading = function (data, headersGetter) {
            $('loadingDiv').show();
            return data;
        };
        $httpProvider.defaults.transformRequest.push(loading);

        $routeProvider.
        when('/', {
            templateUrl: 'elements/home/home.html',
            controller: homeCtrl
        });
   });

解决方案

You could use something like:

.run( function($rootScope, $location) {
   $rootScope.$watch(function() { 
      return $location.path(); 
    },
    function(a){  
      console.log('url has changed: ' + a);
      // show loading div, etc...
    });
});

这篇关于其中函数被调用AngularJS每次网址的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆