php—json响应中的数据在分配给示例变量后不显示

lokaqttq  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(302)

所以,我的水疗中心有98%的功能。该应用程序从mysql数据库中提取数据,并允许用户编辑/删除记录。对于有问题的页面,它将编辑/删除指定学号的数据,但不会在文本字段中正确显示数据。
如果不输入值,它将显示json数组中的第一个项,而不是在搜索字段中指定的项。
我没有很好地解释这一点,但这里是html页面,它使用一个函数将数据传递给控制器,然后控制器按id选择相应的student并将其分配给变量$scope.student。然后,我尝试使用student.first\u name(或任何属性)在html页面上显示学生数据,但它不能正常工作。

<h3>Edit/Delete Student with ID: {{student.student_id}}</h3>

<div class="form-group">
<label for="sid">Student ID:</label>
<input type="text" class="form-control" id="sid" ng-model="sid">
</div>

<p><button type="button" class="btn btn-primary" ng-click="getRecord(sid)"> 
Get Student Info </button> </p>

<div class='row'>

<div class="col-md-6">

    <div class="form-group">
        <label for="first_name">First Name:</label>
        <input type="text" class="form-control" id="first_name" ng-model="student.first_name">
    </div>
    <div class="form-group">
        <label for="last_name">Last Name:</label>
        <input type="text" class="form-control" id="last_name" ng-model="student.last_name">
    </div>
    <div class="form-group">
        <label for="hrs_completed">Hrs Completed:</label>
        <input type="text" class="form-control" id="hrs_completed" ng-model= "student.hrs_completed">
    </div>
    <div class="form-group">
        <label for="hrs_attempted">Hrs Attempted:</label>
        <input type="text" class="form-control" id="hrs_attempted" ng-model= "student.hrs_attempted">
    </div>

</div>

<div class="col-md-6">

    <div class="form-group">
        <label for="gpa_points">GPA Points:</label>
        <input type="text" class="form-control" id="gpa_points" ng-model= "student.gpa_points">
    </div>
    <div class="form-group">
        <label for="major">Major:</label>
        <input type="text" class="form-control" id="major" ng-model="student.major">
    </div>
    <div class="form-group">
        <label for="advisor_id">Advisor ID:</label>
        <input type="text" class="form-control" id="advisor_id" ng-model="student.advisor_id">
    </div>
    <div class="form-group">
        <label for="email">Email:</label>
        <input type="text" class="form-control" id="email" ng-model="student.email">
    </div>

</div>

<button type="button" class="btn btn-primary" ng-click="updateRecord()">Update</button> &nbsp;
<button type="button" class="btn btn-primary" ng-click="deleteRecord()">Delete</button>

下面是我的javascript页面上的控制器:

app.controller('editCtrl', function($scope, $http) {

$http.get("getStudentData.php")
    .then(function (response) {
        $scope.students = response.data;
    });

$scope.getRecord = function(sid) {
    id = sid;

    $scope.student = $scope.students.find(s=>s.id == sid);

};

我需要向服务器发出一个单独的get请求才能正常工作,还是只需要以不同的方式引用student对象?

ntjbwcob

ntjbwcob1#

我认为你现在得到的是不匹配的 response.data 你想做什么 .find() 然后是html上绑定的内容。
看看这个。
您正在尝试通过搜索进行渲染 id 财产。

$scope.students.find(s=>s.id == sid);

当您在ui上使用 student_id 财产。

{{student.student_id}}

所以,你所得到的肯定是不匹配的 response.data 以及您正在使用 $scope.student 属性。我想我的照片会证明你的功能是正确的。
如果这个答案不起作用,请分享你的答案 response.data .

相关问题