[英]Angularjs display dynamic ID and info of particular User in $scope when login with Service and API to fetch in MySQL
我在AngularJS UI上遇到了這個問題。 我創建了一個服務來獲取get方法api / member / item / + id,其中我的參數id是數據庫表中指定的userID。 我已經設置了控制器,但一直出現錯誤。 當我將值聲明為1時,以某種方式設法獲得用戶的ID。但是我試圖使其動態化,這給了我錯誤。
控制者
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
,這是錯字嗎?
為了幫助您調試問題。 我已經模擬了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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.