如何使用Angular JS正确迭代数组元素?

时间:2014-06-10 17:06:35

标签: javascript html angularjs

我想渲染一个表,为数组中的每个对象添加行。

我有一个控制器,如:

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

    var that = this 


    that.users = []


    that.queryUsers = function() {
        console.log("I'm being triggered")

        $http.get("/existingUsers").

        success(function (data,status,headers,config) {
            console.log(data)
            that.users = data
            console.log(that.users)
        }).

        error(function (data,status, headers, config) {
            console.log(status)
        })
    }

})

表格标记:

<table class="table table-bordered">
        <th ng-repeat="th in ['mail','Administrador','Miembro desde']">{{th}}</th>

        <tr ng-repeat="p in signup.users">
            <td>{{p._id}}</td>
            <td>{{p.mail}}</td>
            <td>{{p.admin}}</td>
        </tr>

</table>

Ttable在ng-controller="SignUpController as signup"的div中。当我单击按钮时,我触发queryUsers实际上在浏览器控制台中看到结果:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

mail_id都是每个对象的现有属性。

因此AJAX正在完成并且我应该迭代的数组并且渲染到HTML行实际上存在并且被填充,但是没有显示行。

为什么?

修改

我尝试不修改范围:

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

    $scope.users = []

    $scope.queryUsers = function() {

        console.log("I'm being triggered")

        $http.get("/existingUsers").

        success(function (data,status,headers,config) {
            console.log(data)
            $scope.users = data
            console.log($scope.users)
        }).

        error(function (data,status, headers, config) {
            console.log(status)
        })
    }

})


<div class="tab-pane" id="usecase11" ng-controller="SignUpController">

        <h3>Colaboradores</h3>

        <div class="row">

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

                <table class="table table-bordered">
                        <th ng-repeat="th in ['mail','Administrador','Miembro desde']">{{th}}</th>

                        <tr ng-repeat="p in users">
                            <td>{{p._id}}</td>
                            <td>{{p.mail}}</td>
                            <td>{{p.admin}}</td>
                            <td style="border:none;"><a class="close" ng-click="">$</a></td>
                            <td style="border:none;"><a class="close" ng-click="">*</a></td>
                        </tr>

                </table>
            </div>
        </div>
</div>

然而,我再次看到这样的数组在浏览器控制台上打印但没有呈现给HTML

以下是调用queryUsers的证据,而$scope.users正在获取某些

enter image description here

有趣的是我得到了:{{users}}就在桌子后面,它显示了一个空数组。

enter image description here

以防万一这是GET处理服务器代码:

app.get('/existingUsers',function (request, response) {


    membersCollection.find({},{"password":0}, function (err, data) {

        if (err) response.send(404)

        else {

            data.toArray(function (err, docs) {
                if (err) {

                    console.log(error)
                    response.send("Error")
                }

                response.send(JSON.stringify(docs, null, 4))
            })
        }
    })
}

1 个答案:

答案 0 :(得分:2)

您无法修改$scope。这是更正后的代码:

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

    $scope.users = []

    $scope.queryUsers = function() {
        console.log("I'm being triggered")

        $http.get("/existingUsers").

        success(function (data,status,headers,config) {
            console.log(data)
            $scope.users = data
            console.log($scope.users)
        }).

        error(function (data,status, headers, config) {
            console.log(status)
        })
    }

})

HTML:

<table class="table table-bordered">
    <th ng-repeat="th in ['mail','Administrador','Miembro desde']">{{th}}</th>

    <tr ng-repeat="p in users">
        <td>{{p._id}}</td>
        <td>{{p.mail}}</td>
        <td>{{p.admin}}</td>
    </tr>

</table>
相关问题