0

i wrote a directive that give out some clickable divs for a kicker game, it look like that:

<span>Team 1</span>
<scoreDisplay>
    <div ng-click="setScore(0)" class="score-item">1</div>
    <div ng-click="setScore(1)" class="score-item">2</div>
    <div ng-click="setScore(2)" class="score-item">3</div>
    <div ng-click="setScore(3)" class="score-item">4</div>
    <div ng-click="setScore(4)" class="score-item">5</div>
    <div ng-click="setScore(5)" class="score-item">6</div>
</scoreDisplay>

<span>Team 2</span>
<scoreDisplay>
    <div ng-click="setScore(0)" class="score-item">1</div>
    <div ng-click="setScore(1)" class="score-item">2</div>
    <div ng-click="setScore(2)" class="score-item">3</div>
    <div ng-click="setScore(3)" class="score-item">4</div>
    <div ng-click="setScore(4)" class="score-item">5</div>
    <div ng-click="setScore(5)" class="score-item">6</div>
</scoreDisplay>

after a click on a score-item, i want to get the index of the directive, where the items inside, here the scopefunction inside the controller:

$scope.game = {
    team_1:{name:'Ateam',score:0}
    team_2:{name:'Bteam',score:0}
};
$scope.setScore = function(itemindex, directiveindex){
    $scope.game["team_"+(directiveindex+1)].score = (itemindex+1)

}

any idea, how i can get the directive-index (the parent elemment dom index) with angular? thanks for helping.

1
  • Use '$index' for this purpose and 'ng-repeat' directive. Commented Sep 27, 2014 at 17:26

2 Answers 2

0

Use ng-repeat directive for code parts like:

    <div ng-click="setScore(0)" class="score-item">1</div>
    <div ng-click="setScore(1)" class="score-item">2</div>
    <div ng-click="setScore(2)" class="score-item">3</div>
    <div ng-click="setScore(3)" class="score-item">4</div>
    <div ng-click="setScore(4)" class="score-item">5</div>
    <div ng-click="setScore(5)" class="score-item">6</div>

You can replace in controller:

$scpe.teams = [...]

In template:

<div ng-repeat="team in teams" ng-click="setScore(team.score,$index)" class="score-item">{{team.name}}</div>

You can make similar change for scoreDisplay

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

2 Comments

I m using ng-repeat in the directive and i don't want the index of the directive items, i want to found out the directive - index like this: <mydirective index="1"></mydirective> <mydirective index="2"></mydirective>
Not sure I understood your comment... If you want to get directive scope, then angular.element('...').scope() if you want parent index then you need use jquery
0

Found a solution with a attribute "index":

<scoreDisplay index="1"></scoreDisplay>
<scoreDisplay index="2"></scoreDisplay>

This works fine, but i will search for a better solution like this or with something like:

angular.element("body scoreDisplay").index($event.target);

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.