0

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.

4
  • Can you add the HTML as well and maybe a fiddle demonstration? Commented Feb 29, 2016 at 15:44
  • look my answer. I think this answer solved your problem Commented Feb 29, 2016 at 15:55
  • Couldn't get it to work yet: here is the complete code, which I copy in the w3schools editor (link in html section) jsfiddle.net/6jmLmyek this one is the working: jsfiddle.net/Lu4cv1yn Commented Feb 29, 2016 at 16:40
  • I fixed it :) jsfiddle.net/fhhwLcy9 I had to change some things in the html aswel. I used all the solutions below combined and it works, so thanks everyone! But Sandeep you indeed had the best solution :) Commented Feb 29, 2016 at 16:51

4 Answers 4

1

if you get continues $scope variable then apply this method use it :

$scope['spec'+i];

not

var spec = $scope.spec + i;

$scope['spec'+i]; use it all place where you get $scope variable continues...

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need js code for toggle a div.

Html is adequate.

<div ng-show="showdiv1" ng-click="showdiv1=!showdiv1"></div>

<div ng-show="showdiv2" ng-click="showdiv2=!showdiv2"></div>

Comments

1

Just as an example, maybe something like this?

JS

$scope.specs = [0, 1, 2, 3];

$scope.toggle = function (index) {
    $scope.specs[index] = !$scope.specs[index];
}

Markup

<div ng-repeat="spec in specs">
    <button ng-click="$scope.toggle($index)">Toggle</button>
</div>

Comments

1

You should use an array for specs and a toggle function with index as argument.

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) {

  $scope.spec = [];
  for(var i = 0; i < indexonderdelenlijst; i++) {
    $scope.spec.push(true);        
  }
  $scope.togglespec = function(index) {
    $scope.spec[index] = !$scope.spec[index];
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.