我在 AngularJS UI 上遇到了这个问题。我创建了一个获取方法 api/member/item/+ id 的服务,其中我的参数 id 是我的数据库表中指定的 userID。我已经设置了一个 Controller ,但我一直收到错误消息。当我将值声明为 1 时,我以某种方式设法获取了用户的 ID。但我试图使其动态化,但它给了我错误。
Controller
app.controller('mainCtrl', function($scope, $window, $rootScope, adminService){
$scope.usertype = localStorage.usertype;
$rootScope.usertype = localStorage.usertype;
$rootScope.userID = localStorage.userID;
$scope.username = localStorage.username;
// $scope.userID = 1;
$scope.id= $scope.userID;
// $scope.userID = localStorage.userID;
adminService.editMember($scope.id)
.then(function(data){
//array
$scope.id = data.data[0].userID;
$scope.f_name = data.data[0].first_name;
$scope.email = data.data[0].email;
});
});
服务
function editMember(id) {
return $http({
method: 'get',
url: rootURL + '/member/item/' + id,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
}
MemberInfo.html
<div class="card-body">
<h6 class="card-title mb-1 large">
<a href="#" class="col-xs-10" >
<h4>FirstName: {{f_name}}, </h4>
<label >ID: {{uid}}</label>
</a>
<label class="btn btn-primary pull-right" name="editInfo"><input type="checkbox" class="hidden" for="editInfo" name="editInfo" ng-model="editInfo"> <ic class="fa fa-pencil"></i></label>
<form name="myForm" ng-submit="saveProfile()">
<label>{{uid}}</label>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label>Role : </label>
<input type="text" class="form-control" name="role" ng-model="usertype" readonly>
</div>
<div class="col-md-6">
<label>Email : </label>
<input type="email" class="form-control" name="email" ng-model="email" ng-readonly="!editInfo">
</div>
</div>
</div>
</form>
</div>
最佳答案
问题是您正在定义 uid
但 uid
甚至不存在于 Controller 中,这是错字吗?
帮助您调试问题。我模拟了 HTTP 请求,代码看起来不错,除了这个错字。
之前:
<div class="card-body">
<h6 class="card-title mb-1 large">
<a href="#" class="col-xs-10" >
<h4>FirstName: {{f_name}}, </h4>
<label >ID: {{uid}}</label>
</a>
<label class="btn btn-primary pull-right" name="editInfo"><input type="checkbox" class="hidden" for="editInfo" name="editInfo" ng-model="editInfo"> <ic class="fa fa-pencil"></i></label>
<form name="myForm" ng-submit="saveProfile()">
<label>{{uid}}</label>
之后:
<div class="card-body">
<h6 class="card-title mb-1 large">
<a href="#" class="col-xs-10" >
<h4>FirstName: {{f_name}}, </h4>
<label >ID: {{id}}</label>
</a>
<label class="btn btn-primary pull-right" name="editInfo"><input type="checkbox" class="hidden" for="editInfo" name="editInfo" ng-model="editInfo"> <ic class="fa fa-pencil"></i></label>
<form name="myForm" ng-submit="saveProfile()">
<label>{{id}}</label>
关于javascript - 当使用服务和 API 登录以在 MySQL 中获取时,Angularjs 在 $scope 中显示特定用户的动态 ID 和信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509265/