$ location.path()在AngularJS中不起作用

时间:2016-01-25 14:21:53

标签: javascript angularjs

我一直在尝试将我的应用程序重定向到以下链接 $ location.path(' / enterprise / update /' + enterprise.id);

单击按钮时的当前网址为http://localhost:8080/#/enterprise/view 我希望将其更改为http://localhost:8080/#/enterprise/update/0
(这里的0代表id)

编辑功能是当我点击按钮重定向时调用的功能。

相关代码: 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: '/'
      });
    }
]);

我得到的错误是

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)

1 个答案:

答案 0 :(得分:9)

您的依赖注入数组/参数出错。应该是:

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

注意,每个单个数组元素服务如何对应于函数中的参数。