I am trying to simplify a script with a loop, but I can't get it to work.
I want to convert the following working code and make it shorter to show/hide a div.
var app = angular.module('myApp', ['ngAnimate']);
app.controller('onderdelenCtrl', function($scope) {
$scope.spec1 = true;
$scope.spec2 = true;
$scope.spec3 = true;
...
$scope.spec9 = true;
$scope.togglespec1 = function() {
$scope.spec1=!$scope.spec1; }
$scope.togglespec2 = function() {
$scope.spec2=!$scope.spec2; }
$scope.togglespec3 = function() {
$scope.spec3=!$scope.spec3; }
...
$scope.togglespec9 = function() {
$scope.spec9=!$scope.spec9; }
});
Into something like:
var onderdelenlijst = ["processor", "moederbord", "video", "opslag", "geheugen", "behuizing", "voeding", "dvd", "geluid"];
var indexonderdelenlijst = onderdelenlijst.length;
var app = angular.module('myApp', ['ngAnimate']);
app.controller('onderdelenCtrl', function($scope) {
i=0;
while (i < indexonderdelenlijst) {
i++;
var spec = $scope.spec + i;
spec = true;
togglespec = $scope.togglespec + i;
togglespec = function() {
spec =! spec; }
}
});
I don't know what I am doing wrong, I have tried multiple things, but it keeps failing somehow.