0

I would like to define 4 different outputs. In my HTML I want to say how many columns I would like to show, then in JavaScript I would run this code if there are 4 columns:

<div class="col-md-3">
    <h1>Content</h1>
</div>
<div class="col-md-3">
    <h1>Content</h1>
</div>
<div class="col-md-3">
    <h1>Content</h1>
</div>
<div class="col-md-3">
    <h1>Content</h1>
</div>

Run this code if there are 3 columns:

<div class="col-md-4">
    <h1>Content</h1>
</div>
<div class="col-md-4">
    <h1>Content</h1>
</div>
<div class="col-md-4">
    <h1>Content</h1>
</div>

Run this code if there 2 columns:

<div class="col-md-6">
    <h1>Content</h1>
</div>
<div class="col-md-6">
    <h1>Content</h1>
</div>

Run this code if there is 1 column:

<div class="col-md-12">
    <h1>Content</h1>
</div>

Is there a way doing this with AngularJS?

2
  • Can simply do it on the template side and use ng-if with something like ng-if="colCount == 2" and so on. Commented May 27, 2016 at 11:16
  • 2
    How are you deciding the number of columns? Dont know angular but run a loop and set the col-md- class to 12/no of columns Commented May 27, 2016 at 11:27

1 Answer 1

3

Can you try this:

NG:

$scope.number = 4;//your column count 
$scope.getNumber = function(num) {
    return new Array(num);   
}

Html:

<div ng-repeat="i in getNumber(number) track by $index" class="col-sm-{{12/number}}">
     <h1>Content</h1>
</div>

http://jsfiddle.net/3ck8vb1s/1/

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

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.