使用Angular加载新内容时,我正在尝试更新URL。我只有以下内容我收到错误$ location未定义?

app.controller('mainController', function($scope) {
   $scope.title = 'Services';
   $scope.message = 'Full-service support';
   $location.path('/user/' + $scope.userId, false);
});


全JS

    var app = angular.module('app', ['ngRoute']);

    app.config(function($routeProvider) {

      $routeProvider

      .when('/', {
        templateUrl: 'pages/home.html',
        controller: 'mainController'
      })

      .when('/personal', {
        templateUrl: 'pages/personal.html',
        controller: 'personalController'
      })

      .when('/contacts', {
        templateUrl: 'pages/contacts.html',
        controller: 'contactsController'
      });

    });

    app.controller('mainController', function($scope, $location) {
        $scope.title = 'Services.';
        $scope.message = 'Full.';
        $location.path('/user/', false);
    });

    app.controller('personalController', function($scope, $location) {
        $scope.title = 'Services.';
        $scope.message = 'Full.';
        $location.path('/user/', false);
    });

    app.controller('contactsController', function($scope, $location) {
        $scope.title = 'Services.';
        $scope.message = 'Full.';
        $location.path('/user/', false);
    });

最佳答案

$location不是全局变量。...您需要在打算使用它的地方注入它

app.controller('mainController', function($scope, $location) {
                                                  //^^ inject $location
   $scope.title = 'Services';
   $scope.message = 'Full-service support';
   $location.path('/user/' + $scope.userId, false);
});

10-06 05:27
查看更多