使用AgularJS在setter中保存数组值

时间:2016-05-02 13:27:31

标签: javascript angularjs json onsen-ui monaca

我有一个使用Onsen UI,Monaca和AngularJS的跨平台应用程序。

我有一个页面,其中包含许多在运行时填充的复选框。我有一个方法来检查选中了哪个复选框,然后返回在数据库中分配给它的复选框ID。

构建列表的视图如下所示:

<ul class="list">
    <li class="list__item" ng-repeat="checkItemDescription in data">  
            {{checkItemDescription.checkitemdesc}}

        <label class="switch switch--list-item">
            <input type="checkbox" 
                    class="switch__input" 
                    ng-click="toggleSelection(checkItemDescription.fleetcheckitemid)">
            <div class="switch__toggle"></div>
        </label>
    </li>        
</ul>

列表已构建,我可以在视图控制器中使用以下方法检测何时切换开关

app.js

app.controller("VehicleCheckController", function($scope, $http, sharedProperties)
{
    // Get the list of checkboxes from Database here...

    // Initialising the selected items Array
    $scope.selection = [];

    // Toggle selection for a given Check Item by name
    $scope.toggleSelection = function toggleSelection(fleetCheckItemID)
    {
        var idx = $scope.selection.indexOf(fleetCheckItemID);

        // Is currently selected
        if (idx > -1) 
        {
            $scope.selection.splice(idx, 1);
            console.log("$scope.selection: " + $scope.selection);

            // Set the Fleet Checked Selection ID to the new value
            //setFleetCheckedSelectionID($scope.selection);
            // NOT WORKING HERE
        }
        // Is newly selected
        else 
        {
            $scope.selection.push(fleetCheckItemID);
            console.log("$scope.selection: " + $scope.selection);

            // Set the Fleet Checked Selection ID to the new value
            //setFleetCheckedSelectionID($scope.selection);
            // NOT WORKING HERE
        }
    }
});

然后我有一个服务来处理getter和setter,以便在我的各种控制器之间共享所有值。但是当我尝试在我的服务中为我的服务中设置$ scope.selection数组的值时,我得到一个错误,说该数组是未定义的。

// Stores the varibale $scope.fleetIDSearch form the VehicleIDController to be passed to the API URL call in VehicleCheckController
app.service('sharedProperties', function()
{
    // Initialise the Variables 
    var fleetCheckedSelectionID = [];

    return {
        // Get and set the Fleet Checked Selected ID number
        getFleetCheckedSelectionID: function()
        {
            return fleetCheckedSelectionID;
        },
        setFleetCheckedSelectionID: function(value)
        {
            fleetCheckedSelectionID = value;
        }
    };
});

如何设置$ scope.toggleSelection方法生成的值,以便将它们发送到下一个屏幕?

0 个答案:

没有答案