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

标签 javascript angularjs

我一直在尝试使用以下链接将我的应用程序重定向到以下链接 $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)

最佳答案

您的依赖项注入(inject)数组/参数乱序。应该是:

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

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

关于javascript - $location.path() 在 AngularJS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994869/

相关文章:

javascript - 带数字的变量在警报中不起作用

javascript - Mithril 不会自动重绘 View

css - Bootstrap 导航栏下拉菜单在 iphone 上不起作用

angularjs - Angular 服务和 Web Worker

javascript - 在创建循环的 angularjs 指令中重置范围间隔

javascript - 当innerHTML为空时隐藏div

javascript - 外部 js 文件中的谷歌分析

javascript - ms crm 2011 使用 javascript 检索报价详细信息

javascript - angular,访问 ng-view 包装 div 之外的范围

javascript - BreezeJs 功能已经由 AngularJs 实现了吗?