var demoApp = angular.module('myApp', []);
demoApp.controller('MyController', function($scope, $http) {
$scope.formData = {};
$scope.formData.selectedTechnologies = {};
$scope.checkSelected = function(object) {
return Object.keys(object).some(function(key) {
return object[key];
});
};
$scope.technologies = [
{id:1, name:'Angular JS'},
{id:2, name:'PHP'},
{id:3, name:'HTML'},
{id:4, name:'JAVA'},
{id:5, name:'CSS'},
{id:6, name:'DOTNET'}
];
//$scope.formData.selectedTechnologies = {'Angular JS':true, 'HTML':true}; /* This is working fine */
var myTech = {"myTechnology":["Angular JS","HTML"]}; /* This is dynamic value */
$scope.formData.selectedTechnologies = myTech.myTechnology;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<h2>Technologies</h2>
<div ng-controller="MyController">
<form name="projectForm" novalidate ng-submit="projectForm.$valid && submitForm()">
<div class="col-sm-3" ng-repeat="technology in technologies">
<div class="checkbox checkbox-info checkbox-inline">
<input type="checkbox" name="technology_name" ng-model="formData.selectedTechnologies[technology.name]" id="technology{{$index}}" ng-required="!checkSelected(formData.selectedTechnologies)" />
<label for="technology{{$index}}" ng-bind="technology.name"></label>
<div class="col-lg-12" ng-show="projectForm.$submitted || projectForm.technology_name.$touched">
<span ng-show="projectForm.technology_name.$error.required" class="text-danger">Select any technology.</span>
</div>
</div>
</div>
</form>
</div>
</body>
I have displayed a list of my current technologies. I need to automatically select Angular JS
and HTML
from my variable myTech
.
When I set
$scope.formData.selectedTechnologies = {'Angular JS':true, 'HTML':true};
it works correctly.
Can anyone advise me on how to convert ["Angular JS","HTML"]
to {"Angular JS":true,"HTML":true}
?