$location.path() not working in AngularJS
我一直在尝试使用以下方法将我的应用程序重定向到以下链接:
$ location.path('/ enterprise / update /'enterprise.id);
单击按钮时的当前URL是http:// localhost:8080 /#/ enterprise / view
我希望将其更改为http:// localhost:8080 /#/ enterprise / update / 0
(这里的0代表ID)
编辑功能是单击按钮进行重定向时调用的功能。
相关代码:
EnterpriseCtrl.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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: '/' }); } ]); |
我得到的错误是
1 2 3 4 5 6 7 8 | 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. (angular.js:22945) at HTMLButtonElement.eventHandler (angular.js:3009) |
您的依赖项注入数组/参数不正确。应为:
1 2 | [ '$scope', 'Enterprise', '$location', '$http', '$route', '$routeParams', '$resource', function($scope, Enterprise, $location, $http, $route, $routeParams, $resource) { |
注意,每个单个数组元素服务如何与函数中的参数相对应。