Javascript 在内容更改时使用angular更新URL

Javascript 在内容更改时使用angular更新URL,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我正在尝试用Angular加载新内容时更新我的URL。我只有以下几点我得到的错误$location没有定义 app.controller('mainController', function($scope) { $scope.title = 'Services'; $scope.message = 'Full-service support'; $location.path('/user/' + $scope.userId, false); }); 完整JS var

我正在尝试用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);
});

$location
不是全局性的……您需要将它注入到您想要使用它的地方

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

我已经添加了这个,但是站点现在呈现为空白,我没有收到任何控制台错误或者对你的应用程序配置了解不够我已经添加了这个,但是站点现在呈现为空白,我没有收到任何控制台错误或者对你的应用程序配置了解不够