2016-09-23 6 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

あなたは試すことができますjqLit​​eと私はChromeで読んでいるものを、あなたカントを使用セレクタとして

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

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

関連する問題