2016-09-23 70 views
-1

我有以下代码:获取AngularJs中的特定元素

angular.element('div');

它返回页面上所有div元素的对象。如何获得该对象中特定div.one的索引?我试过indexOf(),但它不适用于对象。

https://jsfiddle.net/quodm9mx/

+1

在这里回答http://stackoverflow.com/questions/23609171/how-to-get-element-by-classname-or-id –

回答

1

您可以尝试

angular.element(document.querySelector('.one')); 

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('GreetingController', ['$scope', function($scope) { 
 
    console.log($(angular.element(document.querySelector('.one'))).index());      
 
     
 
     console.log(angular.element(document.querySelector('.one'))); 
 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body ng-app="myApp" ng-controller="GreetingController"> 
 
    <div></div> 
 
    <div></div> 
 
    <div></div> 
 
    <div class="one"></div> 
 
    <div></div> 
 
    <div></div> 
 
    <div></div> 
 
    <div></div> 
 
    <div></div> 
 
    <div></div> 
 
</div>

+0

谢谢,多数民众赞成正是我需要:) – gespinha

+0

感谢您的投票,你能理由? –

-1

正如我正在读在Chrome中,你不能使用选择与jqLit​​e:

Uncaught Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element

但是,如果您阅读了该链接,它会告诉您,如果jQuery可用(您的页面中包含jQuery),angular.element将成为jQuery选择器($)的别名。所以包括jQuery。

+0

谢谢你-1 ... – matiaslauriti