使用表单提交的数组ng-repeat复选框

时间:2015-06-25 11:20:10

标签: javascript jquery angularjs

enter image description here
  我想在提交event.i上使用mongodb database.console重新获得复选框值我正在获得这样的价值轮胎,备件,附件.i根据输出制作了查看页面。如果我点击复选框我在控制台出错了 TypeError:无法分配给'tyres'的只读属性'选择'。我可以解决这个问题,请一些人帮帮我

'use strict';

/**
 * @ngdoc object
 * @name test1.Controllers.Test1Controller
 * @description Test1Controller
 * @requires ng.$scope
 */
angular
    .module('test1')
    .controller('Test1Controller', [
        '$scope', '$http', '$location', '$window',
        function($scope, $http, $location, $window) {

            $http.get('***').success(function(data, status, response) {
                $scope.items = (JSON.stringify(data[0].D_Services).replace(/\"/g, "")).split(',');
                console.log($scope.items);
            });

            $scope.check = function(items) {
                console.log(items);
            };
        }
    ]);
<div ng-controller="Test1Controller" data-ng-init="loadservice()">
    <div ng-repeat="item in items">
        <input type="checkbox" ng-model="item.selected" ng-true-value="'Y'" ng-false-value="'N'" /> {{item}}
    </div>
    <input type="button" name="submit" value="submit" ng-click="check(items)" />
</div>

how can i grab all selected check box values on submit action only

2 个答案:

答案 0 :(得分:1)

此代码的结果:items.split(',')是一个在范围内不存在的数组,因此它不能是ng-repeat指令的可写模型。您应该在您的范围内创建一个数组,如下所示:

$scope.items = (JSON.stringify(data[0].D_Services).replace(/\"/g, "")).split(',');

并在标记中使用此模型

<div ng-repeat="item in items">
    ...
</div>

如果您需要将结果作为字符串,则应在返回之前加入:

$scope.check = function(items) {
   console.log(items.join(','));
};

答案 1 :(得分:0)

因为items是一个像

这样的字符串
var items = "john,grace,peter";

当你执行items.split(',')时,它会产生像

这样的字符串数组
["john","grace","peter"]

当ng-model尝试为每个元素设置检查状态时出错,因为你无法进行

等操作
items[1].selected

因为项目[1]是一个数组。

相关问题