如何使用AngularJS作为对象中的数组来存储核对表中的值?

时间:2015-05-29 01:45:58

标签: javascript arrays angularjs

我有一个显示为清单的数组。根据用户选择,我想将清单的名称存储为第二个对象中的数组。但是我无法弄清楚,如何只存储名称

这是HTML:

<div ng-app="editorApp" ng-controller="editController">
<pre> {{ comm.mysites | json }}</pre>
<div class="checkbox" ng-repeat="mysite in mysites">
    <label>
        <input type="checkbox" ng-model="comm.mysites[mysite]" ng-true-value=" true" ng-false-value="false" />
        {{ mysite }}
    </label>
</div>

JS:

var editorApp = angular.module('editorApp',[]);

editorApp.controller('editController', function($scope) {
   $scope.mysites = ['customer solutions', 'originations', 'back office', 'branch network', 'collections', 'insurance'];

    $scope.comm = {
        title: 'test title',
        content: 'test content',
        'mysites': {}
    };
});

我期待的结果:

$scope.comm = {
            title: 'test title',
            content: 'test content',
            'mysites': {'customer solutions', 'originations', 'insurance'}
        };

提前致谢。这是JSFiddle:http://jsfiddle.net/_fhdamd/6aqpok91/

1 个答案:

答案 0 :(得分:0)

最后,让它运作起来。基本上需要一个$ scope变量来存储检查的值,然后在写入文件之前将该$ scope变量添加到对象。

感兴趣的人的工作样本:http://jsfiddle.net/_fhdamd/gwwwv1e9/3/

HTML

    <div ng-app="editorApp" ng-controller="editController">
    <pre> {{ commSites | json }}</pre>
    <div>
        <label ng-repeat="mysite in mysites">
  <input type="checkbox" checklist-model="commSites" checklist-value="mysite"> {{mysite}}
</label>
    </div>
        <div>

使用Javascript:

var editorApp = angular.module('editorApp',['checklist-model']);

editorApp.controller('editController', function($scope) {
   $scope.mysites = ['customer solutions', 'originations', 'back office', 'branch network', 'collections', 'insurance'];

    $scope.commSites = [];

    $scope.comms = [
        {
            title: 'title one',
            content: 'content one',
            mysites: ['customer solutions','originations']
        },
        {
            title: 'title two',
            content: 'content two',
            mysites: ['customer solutions', 'insurance']
        }
    ];

    $scope.addComm = function() {
        $scope.comms.push({
            title: $scope.newTitle,
            content: $scope.newContent,
            mysites:$scope.commSites
        });
    };
});