在内容更改时使用角度更新URL

时间:2017-10-25 11:31:46

标签: javascript jquery angularjs

我在使用Angular加载新内容时尝试更新我的网址。我有以下情况我只是得到错误$ 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);
    });

1 个答案:

答案 0 :(得分:2)

$location不是全球性的....你需要将它注入你打算使用它的地方

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