john smith
john smith

Reputation: 733

How do you obtain data from an HTML element using AngularJS?

I am currently using 1.2.x version of AngularJs and I'm trying to retrieve the data from the HTML element:

<div ng-controller="mycontroller" data-foo="bar1">
<div ng-controller="mycontroller" data-foo="bar2"></div>

I want to retrieve the values of foo and have a result of [bar1, bar2]. I thought about using $.data(), but want to avoid that. The controller mycontroller is repeated on purpose and is not a mistake. Any advice on how to retrieve the data in the HTML element?

Upvotes: 1

Views: 42

Answers (1)

Artem Petrosian
Artem Petrosian

Reputation: 2954

You can access data attributes within your controller:

app.controller('mycontroller', function ($scope, $attrs) {
    console.log($attrs.foo);
});

Fiddle

Upvotes: 2

Related Questions