sanjeev shetty sanjeev shetty - 6 months ago 20
AngularJS Question

Getting user defined attribute values in angularjs

Html

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


js

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


But I am getting
v.dataname
as undefined.

Answer

You should be using the following attribute to access it:

v.dataset

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: v.dataset.id, name: v.dataset.name, value: v.value, unit: v.dataset.unit});

See the documentation on how to properly use data attributes.