1

I have an array which is displayed as a checklist. Based on the user selection, I want to store the names of the checklist as an array in a second object. However I cannot figure out, how to only store the names

here is the 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>

The 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': {}
    };
});

Expected Result that I am looking:

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

Thanks in advance. Here is the JSFiddle: http://jsfiddle.net/_fhdamd/6aqpok91/

fahadahmed
  • 41
  • 5
  • you want store names without true/false check ? – daremachine May 29 '15 at 01:50
  • IMO, this is a major omission from Angular (that and how repeated ng-form elements are handled). Check out http://vitalets.github.io/checklist-model/ – Phil May 29 '15 at 01:56
  • I suppose this question is duplicated http://stackoverflow.com/questions/14514461/how-can-angularjs-bind-to-list-of-checkbox-values – Rebornix May 29 '15 at 01:57
  • @daremachine yes, just want to store names that are in the mysites array. – fahadahmed May 29 '15 at 01:59
  • you can use great checklist from phil or you can do it with ng-click function i think – daremachine May 29 '15 at 02:04
  • 1
    `'mysites': {'customer solutions', 'originations', 'insurance'}` is invalid object. you cannot create one like this – PSL May 29 '15 at 02:08
  • @PSL it is not an invalid object, otherwise i would be getting a syntax error in a console. – fahadahmed May 29 '15 at 04:41
  • @Phil .. thanks for the checklist-model link. I actually started of with that but I cannot seem to get the values assigned to the object after getting user input. So I decided to create something on my own. – fahadahmed May 29 '15 at 04:44

1 Answers1

0

Finally, got it working. basically needed a $scope variable to store the checked values and then add that $scope variable to the object before writing to the file.

Working sample for those interested: 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
        });
    };
});
fahadahmed
  • 41
  • 5