sanjeev shetty sanjeev shetty - 1 year ago 81
AngularJS Question

Getting user defined attribute values in angularjs


<input type="text" class="fulltextbox saving_field" dataid="1" dataname="temperature" dataunit="&deg; F" ng-model="addVt.Temp.value" />


var tData = [];
angular.forEach(angSel('.saving_field'), function(v, k){
tData.push({id: v.dataid, name: v.dataname, value: v.value, unit: v.dataunit});

But I am getting
as undefined.

Answer Source

You should be using the following attribute to access it:


Also, the data attributes should have a hyphen in them. Like data-name and not dataname.

So HTML would be:

<input type="text" class="fulltextbox saving_field" data-id="1" data-name="temperature" data-unit="&deg; F" ng-model="addVt.Temp.value" />

And in Javascript:

tData.push({id:, name:, value: v.value, unit: v.dataset.unit});

See the documentation on how to properly use data attributes.