I can not get the select working correctly in angularjs -


i tried article recommends select-options working in angularjs. http://gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-elements-with-angular-js-ng-options-initial-selection/

however have got messed how. here fiddlerjs of code http://jsfiddle.net/8faa5/

here html

<!doctype html> <html class="no-js" data-ng-app="testmodule"> <head>     <title></title> </head> <body data-ng-controller="testcontroller">      <h3>test select</h3>     current value: {{ ourdata.currentselected}} <br>      <select ng-init="ourdata._currval = {value: ourdata.currentselected}"             ng-change="ourdata.currentselected = ourdata._currval.value"             ng-model="ourdata._currval"             ng-options="oneitem.value oneitem.disp         oneitem in ourdata.stufffordropdown track oneitem.value"></select>      <!-- javascript  -->     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>     <script src="js/data.js"></script> </body> </html> 

here js/data.js

(function() {     "use strict";      var smalldata = {         stufffordropdown: [             {                 disp: "01-prospect",                 value: 5             },             {                 disp: "02-constituet issue",                 value: 10             }         ],         currentselected: "10"     };      var mymodule = angular.module("testmodule", ['ui.mask']);      mymodule.controller("testcontroller", ["$scope",         function ($scope){             $scope.ourdata = smalldata;         }     ]);  })(); 

first of all, jsfiddle messed up. have defined angularjs twice, once in jsfiddle options , second time in code. overcomplicated code. made simple rewrite of code working in jsfiddle.

<div data-ng-app="testmodule">     <div data-ng-controller="testcontroller">          <h3>test select</h3> current value: {{ ourdata._currval}}         <br>         <select ng-init="ourdata._currval = {value: ourdata.currentselected}"            ng-model="ourdata._currval" ng-options="oneitem.value oneitem.disp         oneitem in ourdata.stufffordropdown"></select>      </div> </div>   (function () {     "use strict";      var smalldata = {         stufffordropdown: [{             disp: "01-prospect",             value: 5         }, {             disp: "02-constituet issue",             value: 10         }],         currentselected: "10"      };      var mymodule = angular.module("testmodule", []);      mymodule.controller("testcontroller", ["$scope",      function ($scope) {         $scope.ourdata = smalldata;     }]);  })(); 

**edit: ok, have revised code according new request int comment. code goes follows (new plunker)

<div data-ng-app="testmodule">     <div data-ng-controller="testcontroller">          <h3>test select</h3> current value: {{ currentitem.value}}         <br>         <select ng-model="currentitem" ng-options="u u.disp u in items track u.value"></select>     </div> </div>  (function () {     "use strict";      var mymodule = angular.module("testmodule", []);      var ctrl = function ($scope) {         $scope.items = [{             disp: "01-prospect",             value: 5         }, {             disp: "02-constituet issue",             value: 10         }];          $scope.currentitem = $scope.items[1];     };      mymodule.controller("testcontroller", ctrl)  })(); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -