Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get binding value of element in angular

say I have this div in a ng-repeat clause:

<div ng-repeat="item in list" class="myDiv" ng-click="doStuff(item)">
    {{item.title}}
</div>

And it's based on this model:

$scope.list = [
    { 'title': 'Item 1', 'someData': 'lalala' },
    { 'title': 'Item 2', 'someData': '666' },
    { 'title': 'Item 3', 'someData': 'qwerty' }
  ];

Then I would end up getting 3 repeated divs.
Now I have an external script analyzing the page and I want to extract the data of the elements.
So for example if I select the first div var div1 = $('div.myDiv:first') I can get the attributes ng-click and ng-repeat from it. I can also use angular.element(div1).data('$binding') to get the binding {{item.title}}.
But what I really want is the individual item data of this particular div: i.e. I want to write a function getItemData(element) that will get div1 and return { 'title': 'Item 1', 'someData': 'lalala' }.

How can this be done?
Please note that I need a solution that would work both with repeater binding and with regular binding. I basically need to know the actual data that is bound to each element.

like image 320
Malki Avatar asked Apr 05 '15 07:04

Malki


1 Answers

You can access element data object using angular.element.scope method:

var div1 = $('div.myDiv:first');
var dataItem = angular.element(div1).scope().item;

Demo: http://plnkr.co/edit/MzQRjdgqIpbe9LGrwMDZ?p=preview

like image 122
dfsq Avatar answered Oct 24 '22 08:10

dfsq