1

I am aware that Angular is used for generating HTML. Setting $scope.items to an array will cause to generate multiple LI items.

In my case, for every element of items, I would like to invoke a Javascript function. The Javascript then adds somthing to the DOM. is not allowed by Angular. What is the best way to invoke a javascript repeatedly using Angular?

Thanks, Yash

1
  • This can be handled by a directive Commented Mar 14, 2014 at 6:29

2 Answers 2

2

You can use ng-init directive wich will invoke custom function every ng-repeat iteration.

<li ng-repeat="member in members" ng-init="myFunc($index)">
    {{member.mId}}   
</li>

and in controller:

$scope.myFunc = function(index){
   console.log(index);
}

http://jsfiddle.net/FuvRQ/1/

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

2 Comments

But this means a LI would also get created for every iteration. I want to ONLY call the javascript and not create an LI.
@Yash Then you don't need to use ng-repeat directive! Just use usual iterator (for in or anggular.forEach).
0

best is to use a directive and perform your javascript function's functionality in

link:

and whatever you want to insert in the DOM can go as plain html in the property of directive

template:

read about directives these are one of the most powerful features of angular

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.