Using AngularJs $index



Background
Today I had a set if divs I created using AngularJs ng-repeat. My challenge was that there was a button in each div that triggered a modal pop-up when clicked. The content of the modal was supposed to be updated from the corresponding element that generated the particular button that was clicked. Hmm...

Solution
Taking advantage of the ng-click attribute and $index, I added this to the button element

ng-click="SetSelected($index)"


$index is a powerful reference that allows us to pass the index of the element that generated the current control to a function.  That way, I was able to fetch the correct element from the original array of elements that generated the set of controls! 

Isn't Angular sweet?

Comments

Popular posts from this blog

Resize or Crop Image before Upload Using HTML5 File API

Exception from HRESULT: 0x80131040

Get Creative With Data Tables: Row Click Events