【问题标题】:$location.path() not working in AngularJS$location.path() 在 AngularJS 中不起作用
【发布时间】:2016-05-01 21:30:40
【问题描述】:

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

点击按钮时的当前url是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)

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    您的依赖注入数组/参数有问题。应该是:

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

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

    【讨论】:

    • 谢谢!它拯救了我的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    相关资源
    最近更新 更多