P
P
Pavel Bezrukov2013-11-30 19:49:35
Angular
Pavel Bezrukov, 2013-11-30 19:49:35

How to get a reference to the current DOM element from Angular Expression?

Good afternoon.
I can’t find any information on how to get a reference to the home element in the attribute of which it is used from Angular Expression.

<div ng-controller="DemoCtrl">
  <ul>
    <li><a demo-id="15" ng-class="{active: arDemos['demo-id'] == ????????}">Привет {{arDemos['demo-id']}}</a></li>
  </ul>
</div>

In general, I came to the conclusion that there is no way, and put the ID directly in html instead of ????????, but this is not elegant.
In general, I would like to understand whether it is possible to somehow get a reference to this inside expression. The home element does not have its own $scope.
Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Damir Makhmutov, 2013-11-30
@doodoo

Make the $scope.activeDemo property in DemoCtrl, and store the active element there, or its ID. Then it will be much easier to do this:

<li><a demo-id="15" ng-class="{active: arDemos['demo-id'] == activeDemo}">Привет {{arDemos['demo-id']}}</a></li>

M
maxaon, 2013-12-02
@maxaon

No. DOM access from angular expression denied. If you need to get the id, then use a directive like this:

module.directive("demoId", function(){
    return function(scope, element, attrs){
            scope.id = attrs.demoId;
    }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question