2015-05-03 7 views
9
definiert

ich AngularJS Pfad ändern wollen, ohne nachzuladen, schauen 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 Controller:

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

Wenn ChangeURL aufgerufen wird, tritt ein Fehler auf: ReferenceError: $location is not defined

Kann mir jemand helfen? Vielen Dank!

Antwort

25

$ Lage in der controller nicht gespritzt, so ändern, nur

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

zu

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

Vielen Dank. Ich habe mich geändert, aber neuer Fehler: TypeError: $ location.path ist keine Funktion. – UNSTABLE

+0

change '$ location.path ('/ sample/gfshdfdsf', false);' nach '$ location.path ('/ sample/gfshdfdsf');' – sol4me

+0

sorry, $ location.path = function (Pfad, reload) ist definiert in .run – UNSTABLE

1

ich die gleichen Fehler und ich löschte die $ rootScope aus den Definitionen. Danach hat es funktioniert. Keine Ahnung warum.

arbeiten nicht

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

Arbeiten

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