0

I am using angular repeat to populate form's fields dynamically. There is a scenario where I need to to notify user(show ! icon for duplicate fields). Below is code snippet:

<div ng-init="counter = 0">
  <div ng-repeat="item in list track by $index">
    <div ng-show="{{item.show}}">{{counter+1}}</div>
  </div>
  <div ng-show="{{counter > 1}}"> ! </div>
</div>

Counter variable only increment if its used like {{counter + $index}}, can it be possible without $index?

8
  • counter = counter + 1 ? Commented Jul 29, 2016 at 7:24
  • 1
    ng-show="item.show" Commented Jul 29, 2016 at 7:37
  • What exactly you're looking for to notify for duplicate values. Post your full code in fiddle. Commented Jul 29, 2016 at 7:52
  • What is "item"? How you tell which "item" is duplicated? It has duplicated ID? It is a number? A string? We can't read someone's mind, though. Commented Jul 29, 2016 at 7:57
  • @MarinTakanov, item has property show which is mentioned in code snippet. ng-show="{{item.show}} Commented Jul 29, 2016 at 18:15

1 Answer 1

2

Assigning variables inside html is not officially supported.

But there is always a hack for what you asked:

<div ng-init="counter = 0"></div>
<div ng-repeat="n in [1,2,3,4,5]">
    <div style="display:none;">{{ n == 3 ? counter = "World!" : counter = n }}</div>
    <p>Hello {{ counter }}</p>
</div>

Notice that I used a non-displayed div for assigning the "counter" conditionally.

Output:
Hello 1
Hello 2
Hello World!
Hello 4
Hello 5

Answer to the 1st comment: When counter == 3, we divide it by 2.

<div ng-init="counter = 0"></div>
<div ng-repeat="n in [1,2,3,4,5]">
    <div style="display:none;">
        {{ counter = n }}
        {{ counter == 3 ? counter = counter / 2 : counter = counter }}
    </div>
    <p>Hello {{ counter }}</p>
</div>

Output:
Hello 1
Hello 2
Hello 1.5
Hello 4
Hello 5

Answer to the 3rd comment:

I finally understood what you asked. Let me change the way to approach by using ng-if to keep the record of counter. I used ng-init to increment the counter when n is divisible by 2. You need to call $parent.$parent.counter to reach the original counter otherwise ng-if will create its own counter inside the child scope.

JSFiddle

<div ng-init="counter = 0"></div>      
<div ng-repeat="n in [2,6,5,6,8,9,11] track by $index">
    <!-- ngRepeat child scope -->
    <div ng-if="n % 2 == 0"
    ng-init="$parent.$parent.counter = $parent.$parent.counter + 1"
    style="display:none;">
      <!-- ngIf also creates a child scope -->
    </div>
</div>      
<p>Counter = {{ counter }}</p>

Output:
Counter = 4
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @KinoP but it will not serve the purpose. Is it possible to get count of number divided by 2 with your example? If yes, please paste code snippet, it will solve my problem.
@crmApex I'm not sure if I understand your problem, but check the updated answer above. The conditional expression of {{ boolean ? true case : false case }} is quite powerful.
Thanks. I want the output: 2 because in your list n [1,2,3,4,5] only 2 & 4 is divisible by 2. Now, consider the fact of dynamic list. Let say n has [ 2,6,5,6,8,9,11], so output: 4 as n has (2,6,6,8) which are divisible by 2. So, basically {{counter}} need to be incremented when an item of n is divided by 2 and remainder = 0.
@crmApex Check it out.
Thanks. I got what I have asked. $parent.$parent.counter, served the purpose.

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.