我有一个显示为 list 的数组。根据用户的选择,我想将 list 的名称作为数组存储在第二个对象中。但是我不知道如何只存储名称
这是 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/
最佳答案
终于成功了。基本上需要一个 $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
});
};
});
关于javascript - 如何使用 AngularJS 将 list 中的值存储为对象中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30519829/