2016-01-20 99 views
0

爲什麼$ location.url(「/ messages」);不管用? AngularJS中的工廠內的$ location是否有任何限制?任何想法是怎麼回事?angularjs導航到工廠/服務路由

如果我做錯了什麼可以請告訴我應該如何重定向到另一個URL「/ messages」?我無法重新加載頁面使用$窗口不是一個選項。

感謝

我廠:

(function() { 
    'use strict'; 

    angular 
     .module('app.conversation') 
     .factory('conversationFactory', conversation); 

    conversation.$inject = ['$rootScope', '$q', 'authFactory', '$location']; 

    function conversation($rootScope, $q, authFactory, $location) { 

     var service = { 
      changeLocation: changeLocation 
     }; 

     return service; 

     function changeLocation() { 
      $location.url("/messages"); // recarga MIERDA!!!!! 
     } 

    } 

})(); 

路線正常工作:

(function() { 
    'use strict'; 

    angular 
      .module('app.messages') 
      .config(configFunction); 

    configFunction.$inject = ['$routeProvider']; 

    function configFunction($routeProvider) { 
     $routeProvider 
       .when('/messages', { 
        templateUrl: 'app/messages/messages.html', 
        controller: 'messagesController', 
        controllerAs: 'meC', 
        resolve: { 
         user: function ($q, $timeout, authFactory) { 
          return authFactory.isLoggedInPromise(); 
         } 
        } 
       }).when('/compose/:to?', { 
        templateUrl: 'app/messages/compose.html', 
        controller: 'composeController', 
        controllerAs: 'mcC', 
        resolve: { 
         user: function ($q, $timeout, authFactory) { 
          return authFactory.isLoggedInPromise(); 
         } 
       } 
     }); 
    } 

})(); 
+0

除非我很困惑,你不使用'角度-UI-router'(第三方庫)所有,而是內置角度路由器。我幾乎發佈了一個ui路由器的具體答案('$ state.go(...)'),然後才意識到這一點... –

+0

我繼續編輯它爲你';-) –

+1

退房: http:// stackoverflow.com/questions/19499323/location-path-doesnt-change-in-a-factory-with-angularjs –

回答

0

使用$location.path("/messages");