javascript - Using ng-model with select to show data attribute instead of value? -


i have simple select list contains both data attributes , value:

<select ng-model="mylist">     <option value="1" data-name="test name 1">option 1</option>     <option value="2" data-name="test name 2">option 2</option>     <option value="3" data-name="test name 3">option 3</option> </select>  <div id="displayselected">     {{mylist}} </div> 

inside div (with id of displayselected), displaying value of selected option.

how can change display data-name attribute instead?

i not sure of complete data structure , other possibilities not mentioned in question. here simple solution static list.

app.directive('dataselector', function(){  return{     restrict:'a',     require:['ngmodel','select'], //require ng-model , select in order restrict used else     link:function(scope, elm, attrs, ctrl){       //get configured data selected, made configurable in order able use other data attribs       var dataprop = attrs.dataselector,           ngmodel = ctrl[0];         elm.on('change', handlechange);//register change listener        scope.$on('$destroy', function(){         elm.off('change', handlechange);       });        function handlechange(){         //get value         var value = this.value;         //get dataset value         var dataval = elm[0].queryselector('option[value="' + this.value + '"]').dataset[dataprop];         //reset ngmodel view value         ngmodel.$setviewvalue(dataval);         ngmodel.$render();         //set element value selects appropriate item         elm.val(value);       }     }   } }); 

and use as:

 <select ng-model="mylist" dataselector="name"> 

note: if using different way(that not showing us) build select options based on data structure has value populated in data attribute (like ng-repeat), register change event using ng-change="selecteditem(itemfromrepeat)" , can set property data value in handler , use it.

var app = angular.module('plunker', []);    app.controller('mainctrl', function($scope) {    }).directive('dataselector', function() {      return {      restrict: 'a',      require: ['ngmodel', 'select'],      link: function(scope, elm, attrs, ctrl) {        var dataprop = attrs.dataselector;        elm.on('change', handlechange);          scope.$on('$destroy', function() {          elm.off('change', handlechange);        });          function handlechange() {          var value = this.value;          var dataval = elm[0].queryselector('option[value="' + this.value + '"]').dataset[dataprop];          ctrl[0].$setviewvalue(dataval);          ctrl[0].$render();          elm.val(value);        }      }    }    });
<!doctype html>  <html ng-app="plunker">    <head>    <meta charset="utf-8" />    <title>angularjs plunker</title>    <script>      document.write('<base href="' + document.location + '" />');    </script>    <link rel="stylesheet" href="style.css" />    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script>    <script src="app.js"></script>  </head>    <body ng-controller="mainctrl">    <select ng-model="mylist" dataselector="name">      <option value="1" data-name="test name 1">option 1</option>      <option value="2" data-name="test name 2">option 2</option>      <option value="3" data-name="test name 3">option 3</option>    </select>    <data-select></data-select>    <div id="displayselected">      {{mylist}}    </div>  </body>    </html>


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -