【问题标题】:How to loop through array with arrays/objects and assign properties new values in Angular?如何使用数组/对象循环遍历数组并在 Angular 中为属性分配新值?
【发布时间】:2017-05-16 23:11:08
【问题描述】:

我有一个数组和对象数组。我有一个函数,我想为属性分配一个值(例如,$scope.companies[0].users 的 'call':'' 成为用户在复选框中选中的任何值)。我对其进行了研究,但我只是不知道该怎么做,到目前为止我所做的一切都是错误的。非常感谢!!

      <form action="" ng-click="change(key)">
        <input ng-model="key.call"type="checkbox"">Call
        <br>
        <input ng-model="key.person"type="checkbox" >Person
        <br>
        <input type="checkbox"ng-model="key.dial">Dial
        <br>
        <input type="checkbox" ng-model="key.voice">Voice          
    </form>

app.controller('appCtrl', function($scope) {
    $scope.companies = [{
        name: 'The Best Company Denim',
        users: [{
            firstName: 'Alex',
            lastName: 'D',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Sarah',
            lastName: 't',
            number: 14,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'J',
            lastName: 'd',
            number: 07,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }, {
        name: 'The Best Company Elegant',
        users: [{
            firstName: 'Alx',
            lastName: 'B',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Seth',
            lastName: 'w',
            number: 12,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'J.S',
            lastName: 'B',
            number: 7.
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }, {
        name: 'The Best Company by Julia',
        users: [{
            firstName: 'Aleddddx',
            lastName: 'l',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Maggy',
            lastName: 'n',
            number: 1,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Ja',
            lastName: 'Key',
            number: 123,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }]

    $scope.change = function(key) {
        for (var i = 0; i < $scope.companies[0].users; i++) {
            $scope.companies[0].users[i].call: key)
    }
}
});

【问题讨论】:

    标签: javascript angularjs object for-loop


    【解决方案1】:

    根据上面的代码,$scope.companies[0].users 是一个数组,它有一个length 属性,可以为您提供其中的项目数。那么您的代码将是:

    $scope.change = function(key) { for (var i = 0; i < $scope.companies[0].users.length; i++) { $scope.companies[0].users[i].call = key; }

    如果你也在寻找循环公司,那么嵌套 2 个 for 循环,一个循环公司,另一个循环用户,与上面代码中给出的想法相同。

    【讨论】:

    • 非常感谢您的回答。我唯一不明白的是,当我 console.log $scope.companies[0].users[i] 例如在复选框中选择“调用”时,它给了我一个对象(所以调用=对象)将 call="" 更改为 call=true。
    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多