简体   繁体   中英

$location.path() not working in AngularJS

I have been trying to redirect my application to the following link using $location.path('/enterprise/update/' + enterprise.id);

The current url when I click the button is http://localhost:8080/#/enterprise/view and I would like it to be changed to http://localhost:8080/#/enterprise/update/0
(The 0 here represents the id)

The edit function is the one which is called when I click on the button to redirect.

Relevant code: EnterpriseCtrl.js

app.controller('EnterpriseCtrl', ['$scope', 'Enterprise', '$location','$http','$route','$routeParams','$resource', function($scope, Enterprise, $http, $route, $location, $rootScope) {

  $scope.enterprises = Enterprise.list({}, function (response) {
        return response;
        });
  $scope.add = function(){
        Enterprise.save($scope.enterprise,function (){});
        $scope.enterprise = null;
    };
  $scope.delete = function(enterprise, index){
    alert("Do you really want to delete an Enterprise at index " + (index+1) + "?");
    //book.$remove();
    Enterprise.remove(enterprise);
    $scope.enterprises.splice(index, 1);
};
    $scope.edit = function(enterprise){
    console.log(enterprise.id);
    console.log(location);
    console.log($location);
    $location.path('/enterprise/update/' + enterprise.id);
   };
   // $scope.enterprise = Enterprise.get({id: $route.current.params.id});
  $scope.update = function(){
    Enterprise.update($route.current.params.id);
    $location.path($rootScope.history.view);
  };

}]);

app.js

var app = angular.module('mpsApp', ['ngRoute','ngResource']);                     
app.config(['$routeProvider','$locationProvider',
  function ($routeProvider,$locationProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'home.html'
      }).
      when('/enterprise/view', {
        controller: 'EnterpriseCtrl',
        templateUrl: 'showEnterprise.html'
      }).
      when('/enterprise/add', {
        controller: 'EnterpriseCtrl',
        templateUrl: 'addEnterprise.html'
      }).
      when('/enterprise/update/:id', {
        controller: 'EnterpriseCtrl',
        templateUrl: 'updateEnterprise.html'
      }).
      otherwise({
        redirectTo: '/'
      });
    }
]);

The error that I get is

TypeError: $location.path is not a function
at Scope.$scope.edit (EnterpriseCtrl.js:29)
at $parseFunctionCall (angular.js:12330)
at callback (angular.js:22940)
at Scope.$eval (angular.js:14381)
at Scope.$apply (angular.js:14480)
at HTMLButtonElement.<anonymous> (angular.js:22945)
at HTMLButtonElement.eventHandler (angular.js:3009)

Your dependency injection array/parameters are out of order. Should be:

[       '$scope', 'Enterprise', '$location', '$http', '$route', '$routeParams', '$resource', 
function($scope,   Enterprise,   $location,   $http,   $route,   $routeParams,   $resource) {

Note, how each single array element service corresponds to the parameters in the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM