How to push objects in AngularJS between ngRepeat arrays

前端 未结 3 1617
无人共我
无人共我 2021-01-31 03:14

So I\'m new to AngularJS and I\'m trying to build a very simple list app, where I can create an ng-repeat item list and then push a selected item into another ng-repeat list. Al

3条回答
  •  醉话见心
    2021-01-31 03:35

    You'd be much better off using the same array with both lists, and creating angular filters to achieve your goal.

    http://docs.angularjs.org/guide/dev_guide.templates.filters.creating_filters

    Rough, untested code follows:

    appModule.filter('checked', function() {
        return function(input, checked) {
            if(!input)return input;
            var output = []
            for (i in input){
                var item = input[i];
                if(item.checked == checked)output.push(item);
            }
            return output
        }
    });
    

    and the view (i added an "uncheck" button too)

    Add Item


    Checked Items: {{getTotalCheckedItems()}}

    Checked:

    amount: {{item.amount}} - name: {{item.name}} - this item is checked!

    Unchecked Items: {{getTotalItems()}}

    Unchecked:

    amount: {{item.amount}} - name: {{item.name}} -

    Then you dont need the toggle methods etc in your controller

提交回复
热议问题