编辑配置文件页面上的 Angularjs 复选框值


Angularjs checkbox value on edit profile page

我是 angularjs 的新手,在编辑配置文件页面上的复选框值上遇到了一些问题。

我有一个复选框的数组

$scope.checkBoxes = [
        {id:'1', value:'Reading'},
        {id:'2', value:'Cooking'},
        {id:'3', value:'Dancing'},
        {id:'4', value:'Singing'}
];

并且我使用ng重复显示复选框

<div ng-repeat="checkBox in checkBoxes" style="display:inline;">
    <input type="checkbox" ng-model="checkBox.selected" id={{checkBox.id}}' ng-checked="checkItem(checkBox.id)" ng-change="listActionsHandler(checkBoxes)">{{ checkBox.value }}
</div>

这是我的 ng-change 函数:

$scope.listActionsHandler = function(list) {
        var has = [];
        angular.forEach(list, function(item) {
            if ( angular.isDefined(item.selected) && item.selected === true ) {
                has.push(item.id);
            }
        });
        formData['hobby'] = has;
 }

这是我的ng-checked函数,用于根据数据库保存的值显示复选框选中。

var arr_checked_items = data.data.hobby;
var arraySize = arr_checked_items.length;
$scope.checkItem = function (id) {
    var checked = false;
    for(var i=0; i<= arraySize; i++) {
        if(id == arr_checked_items[i]) {
                checked = true;
        }
    }
    return checked;
};

这些功能在添加和编辑页面上正常工作,但问题是当我不对复选框应用任何更改并单击提交按钮时,它的值为空白。

我的问题是:如果我不应用任何更改,它应该采用复选框

的旧值,如果我应用任何更改,它应该采用复选框的新值。

请尝试使用以下代码。我不确定天气它是否有效。

$scope.checkItem = function (id) {
    var checked = "";
    for(var i=0; i<= arraySize; i++) {
        if(id == arr_checked_items[i]) {
                checked = "checked";
        }
    }
    return checked;
};