Aryeh Armon
Aryeh Armon

Reputation: 2185

how can i get the data-something attribute with angularJS

I would like to access the {{ test.data('something') }} however it does not work.

<select ng-model="test">
    <option data-something="mySomething" value="blahblah">blahblah</option>
</select>

Is there some way i can access it?

I am using angular 1.3.14 ( current latest )

Upvotes: 2

Views: 768

Answers (1)

devqon
devqon

Reputation: 13997

If you just assign the variable to the attribute, you can always access the variable. That is also how angular is meant to be used:

controller:

// Reference to mySomething
$scope.mySomething = "Some value";

view:

<select ng-model="test">
    <option data-something="mySomething" value="blahblah">blahblah</option>
</select>

When you have multiple options (for example an ng-options) you can do the following:

controller:

$scope.myOptions = [
    { mySomething: "Something", value: "1", text: "Value 1" },
    { mySomething: "Something else", value: "2", text: "Value 2"}
];

$scope.doSomething = function() {
    console.log($scope.test.mySomething);
}

view:

<select ng-options="opt.value as opt.text for opt in myOptions"
        ng-model="test"
        ng-change="doSomething()">
</select>

To access the attribute's value inside a directive, see this thread on SO

Upvotes: 1

Related Questions