javascript - 当使用服务和 API 登录以在 MySQL 中获取时,Angularjs 在 $scope 中显示特定用户的动态 ID 和信息

标签 javascript html mysql angularjs api

我在 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>

最佳答案

问题是您正在定义 uiduid 甚至不存在于 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>

JSFiddle Demo

关于javascript - 当使用服务和 API 登录以在 MySQL 中获取时,Angularjs 在 $scope 中显示特定用户的动态 ID 和信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509265/

相关文章:

css - 我希望我的导航栏居中,但我所做的任何事情似乎都不会影响它

javascript - 将每个字母表定位到每个答案容器但不添加新的答案容器 - 使用 AngularJs 拖放

MySQL逻辑问题: Which is the best way to give permission to users to different rows from different tables?

javascript - 为什么这个一元函数在另一个 javascript 函数中?

html - 在 html 3d 中创建一个圆柱体

mysql - 转换不起作用,语法错误

mysql - SQL比较必须确定的2个值

javascript - 如何在 Javascript 中调用祖父类的 setter

javascript - Redux 数据结构

javascript - 未调用 jQuery.ajax 转换器