I'm trying to set a checkbox to true with Angularjs. Right
now I'm cheating using vanilla javascript to manipulate the DOM, but I
want to do it the right way with Angularjs.
Here is the view:
<div x-ng-repeat="addon in addons">
<label for="{{addon.addoncode}}">
<input type="checkbox"
name="{{addon.addoncode}}"
value="{{addon.addoncode}}"
id="{{addon.addoncode}}"
x-ng-model="addon.checked"
x-ng-click="checkAddonDependencies()" >
<span x-ng-bind-html="addon.addon_desc">{{addon.addon_desc}}</span> </label>
</div>
And here is the relevant part of the controller:
if (document.getElementById(dep)) {
document.getElementById(dep).checked=true;
}
The dep value is equal to the addoncode value, so if it exists I
check the box. This works and checks the box but how could I do this
using the scope instead?
I tried:
x-ng-model="addon.addoncode"
with
$scope.addon.dep = true;
But no luck... any help appreciated. Thanks.
Here is the full controller:
rates.controller('getAddons',
function ($scope, $location, filterFilter, myService, $q) {
$scope.loading = true;
$scope.set_addons = false;
myService.addons().then(function (addons) {
$scope.addons = addons;
$scope.loading = false;
$scope.set_addons = true;
});
$scope.checkAddonDependencies = function () {
$scope.loading = true;
var addons = [];
var queries = [];
var arr = filterFilter($scope.addons, {
checked: true
});
for (var i = 0; i < arr.length; i++) {
element = arr[i].addoncode;
addons.push(element);
}
for (var x = 0; x < addons.length; x++) {
queries.push(myService.addon_dependencies(addons[x]));
}
$q.all(queries).then(function (data) {
for (var i = 0; i < data.length; i++) {
if (data[i].length !== 0) {
dep = data[i][0].addon_depend;
if (dep && dep !== null && document.getElementById(dep)) {
document.getElementById(dep).checked=true;
addons.push(dep);
}
}
}
myService.addItem('addons', addons.sort());
});
};
});