我正在研究Angularjs,我遇到了问题,我无法使用$ routeParams从URL获取参数值。
var MyApp = angular.module('testAngularApp', ['ngRoute','ngResource'])
.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/', {
when('/updateUser/:id', {
templateUrl: 'Pages/User/Edit.html',
controller: 'UpdateUserCtrl'
})
}]);
控制器
angular.module('testAngularApp').
controller('UpdateUserCtrl', ['$scope', 'updateUserService', '$http','$route', '$routeParams', function ($scope, updateUserService, $routeParams,$route, $http) {
$scope.testV = "Update User Ctrl";
console.log("hello");
$scope.params1 = $routeParams.id;
console.log($routeParams.id ); //i am getting undefined
console.log($scope.params1); //i am getting undefined
var check = $routeParams['id'];
console.log(check); //i am getting undefined
}]);
答案 0 :(得分:4)
我认为service
的顺序是错误的。尝试更改如下
controller('UpdateUserCtrl', ['$scope', 'updateUserService','$route','$http' '$routeParams',
function ($scope, updateUserService,$route, $http, $routeParams)
答案 1 :(得分:1)
$routeParams
将获取从URL到控制器的参数列表
您定义的函数定义错误。它的定义应与我们为局部变量创建的顺序相同。
controller('UpdateUserCtrl',
['$scope', 'updateUserService', '$http','$route','$routeParams',
function ($scope, updateUserService, $http,$route,$routeParams ) {
// your Script goes Here.
console.log($routeParams.id);
}