【问题标题】:Angular loop is not updating角循环没有更新
【发布时间】:2015-09-11 01:23:20
【问题描述】:

刚开始使用角度。我成功地使用 php 从数据库中保存和检索数据。我也可以在列表项中循环数据,但是当我添加新用户时,列表不会更新,只有当我刷新浏览器时才会显示新添加的用户

这是我的 html 代码:

<div>
    <ul ng-init="get_user()">
        <li ng-repeat="user in userInfo ">{{user.user_name}}<a href="" ng-click="prod_delete(product.id)"> --> Delete</a></li>
    </ul>
</div>

这是我的 app.js 代码:

var app = angular.module('AddUser', ['ui.bootstrap']);

app.controller('AppCtrl', function($scope, $http){
    $scope.userInfo =  [];

    /** function to add details for a user in mysql referecing php **/
    $scope.save_user = function() {

        $http.post('db.php?action=add_user', 
            {
                'user_name'  : $scope.user_name, 
                'user_email' : $scope.user_email
            }
        )

        .success(function (data, status, headers, config) {
            $scope.userInfo.push(data);
            console.log("The user has been added successfully to the DB");
            console.log(data);
        })

        .error(function(data, status, headers, config) {
            console.log("Failed to add the user to DB");
        });
    }

    /** function to get info of user added in mysql referencing php **/
    $scope.get_user = function() {
        $http.get('db.php?action=get_user').success(function(data)
        {
            $scope.userInfo = data;   
            console.log("You were succesfull in show user info"); 
            //console.log(data);
        })
    }

    });

【问题讨论】:

  • 你应该调用$scope.get_user()方法再次从数据库中获取数据
  • OP as parkar 说应该完成,因为 ng-init 只被调用一次。
  • 但 GY22 将 data 推入 userInfo 成功 $http.post
  • data 到底是什么?
  • @pankajparkar 完全同意你的看法。

标签: javascript php mysql angularjs angularjs-http


【解决方案1】:

当您进行调用后,通过服务器方法将数据保存到数据库中,但是在您成功调用后,您将该数据推送到 userInfo 对象中,这在技术上听起来是错误的。

我希望您在 post call 成功后使用 $scope.get_user() 制作一个 ajax 从 db 获取新数据。

代码

$scope.save_user = function() {
    $http.post('db.php?action=add_user', {
       'user_name'  : $scope.user_name, 
       'user_email' : $scope.user_email
    }).success(function (data, status, headers, config) {
       //$scope.userInfo.push(data); //remove this line
        $scope.get_user(); //this will fetch latest record from DB
        console.log("The user has been added successfully to the DB");
        console.log(data);
    }).error(function(data, status, headers, config) {
        console.log("Failed to add the user to DB");
    });
}

【讨论】:

    【解决方案2】:

    尝试将保存用户更改为:

    $scope.save_user = function() {
        $http.post('db.php?action=add_user', {
            'user_name'  : $scope.user_name, 
            'user_email' : $scope.user_email
        })
        .success(function (data, status, headers, config) {
            $scope.userInfo.push(data.data);
            console.log("The user has been added successfully to the DB");
            console.log(data);
        })
        .error(function(data, status, headers, config) {
            console.log("Failed to add the user to DB");
        });
    }
    

    我认为您返回的数据是一个对象,并且您期望的任何数据都是 data.data,因此您将无效对象推送到您的 userInfo 数组

    【讨论】:

    • 我建议从 http 响应推送添加的用户名和电子邮件,以确保正确添加内容,以防您有一些参数名称拼写错误并在某些字段中实际保存为空白。
    • 已更新,我认为问题在于数据是在 data.data 下返回的,因此循环正在寻找的键在他正在推送的对象上不存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多