2015-05-03 15 views
9

voglio AngularJS Cambia percorso senza ricaricare, guarda http://joelsaupe.com/programming/angularjs-change-path-without-reloading/

in Core.js:

'use strict'; 
    angular.module('App',['ngRoute']) 
     .run(['$route', '$rootScope', '$location', function ($route, $rootScope, $location) { 
     var original = $location.path; 
     $location.path = function (path, reload) { 
      if (reload === false) { 
       var lastRoute = $route.current; 
       var un = $rootScope.$on('$locationChangeSuccess', function() { 
        $route.current = lastRoute; 
        un(); 
       }); 
      } 
      return original.apply($location, [path]); 
     }; 
    }]); 

In regolatore:

angular.module('App')   
     .controller('DetailController', ['$scope', '$location', function($scope) { 
    $scope.changeURL = function(){ 
      console.log("IN changeURL"); 
      $location.path('/sample/gfshdfdsf', false); 
     };  
    }]); 

Se invoca changeURL, si verificherà l'errore: ReferenceError: $location is not defined

Qualcuno può aiutarmi? Grazie!

risposta

25

$ posizione non viene iniettato nel controller, quindi basta cambiare

.controller('DetailController', ['$scope', '$location', function($scope) 

a

.controller('DetailController', ['$scope', '$location', function($scope, $location) 
+0

ti ringrazia. Ho cambiato, ma nuovo errore: TypeError: $ location.path non è una funzione. – UNSTABLE

+0

cambia '$ location.path ('/ sample/gfshdfdsf', false);' a '$ location.path ('/ sample/gfshdfdsf');' – sol4me

+0

scusa, $ location.path = function (percorso, ricarica) è definito in .run – UNSTABLE

1

mi è stato sempre lo stesso errore e ho cancellato il $ rootScope dalle definizioni. Dopo che ha funzionato. Non ho idea del perché.

Non funziona

app.factory("OrganizationService", 
    ['$q', '$http', '$log', '$location', '$rootScope', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService', 
    function($scope , $http, $log, $location, $cookies, $rootScope, $timeout) { 

Lavorare

app.factory("OrganizationService", 
    ['$q', '$http', '$log', '$location', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService', 
    function($scope , $http, $log, $location, $cookies, $timeout) { 
Problemi correlati