admin管理员组

文章数量:1289412

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.

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.

Share Improve this question asked Apr 5, 2015 at 7:06 MalkiMalki 2,3858 gold badges34 silver badges62 bronze badges 1
  • With an external script analyzing the page how can only get what you find in the page. So 'someDate' would not be available if not contained in the page, like in your example. Jquery should do the job. – dec Commented Apr 5, 2015 at 7:18
Add a ment  | 

1 Answer 1

Reset to default 10

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

本文标签: javascriptGet binding value of element in angularStack Overflow