AngularJS checkbox 前后台交互

前台根据字典值自动循环出复选框

后台接到用","分隔的字符串

前台代码

{{dict.label}}

angularjs代码

        
        $scope.selected = [];
        var updateSelected = function (action, id) {
            id = "" + id + "";
            if (action == 'add' && $scope.selected.indexOf(id) == -1) {
                $scope.selected.push(id);
            }
            if (action == 'remove' && $scope.selected.indexOf(id) != -1) {
                var idx = $scope.selected.indexOf(id);
                $scope.selected.splice(idx, 1);
            }
        };
        $scope.updateSelection = function ($event, id) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            updateSelected(action, id);
        };
        $scope.isSelected = function (id) {
            return $scope.selected.indexOf(id) >= 0;
        };
        $scope.getRequiremt = function () {
            var str = "";
            for (var i = 0; i<$scope.selected.length; i++){
                str += "," + $scope.selected[i];
            }
            if(str != ""){
                str = str.substring(1);
            }
            return str;
        };

最后把得到的字符串赋给需要传回后台的对象就好了

$scope.dioptroscopy.requiremt = $scope.getRequiremt();

 

你可能感兴趣的:(开发经验)