我尝试在使用 Angular 加载新内容时更新我的 URL。我有以下内容,但我收到错误 $location is not Defined?
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
不是全局变量...您需要将其注入(inject)到您打算使用它的位置
app.controller('mainController', function($scope, $location) {
//^^ inject $location
$scope.title = 'Services';
$scope.message = 'Full-service support';
$location.path('/user/' + $scope.userId, false);
});
关于javascript - 在内容更改时使用 Angular 更新 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46931309/