2

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 )

Aryeh Armon
  • 2,137
  • 2
  • 22
  • 37

1 Answers1

1

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

Community
  • 1
  • 1
devqon
  • 13,818
  • 2
  • 30
  • 45
  • yes, but lets say i have more then one option? how would i know what is the selected one? there data-something attribute may have different values – Aryeh Armon Jun 29 '15 at 08:04
  • See edited answer. You can bind objects to a select and get the selected object through the `ng-model` – devqon Jun 29 '15 at 08:11