javascript - Setting a default value in a dropdown list -


i have dropdown list. objective set dropdown list "default" on load

<b>filter within months:</b> <select  class="btn green" ng-model="myoptions" ng-options="i.label in items track i.id" ng-change="selecteditemchanged()"> </select> 

controller.js setting model "default" on load.

$scope.myoptions.id = 0; $scope.items = [{   id: 0,   label: 'default' }, {   id: 1,   label: 'all' }, {   id: 2,   label: '3 month' }, {   id: 3,   label: '6 month' }, {   id: 4,   label: 'previous month' }];  $scope.selecteditemchanged = function() {   var date = new date();   var enddate = date.now();   var firstday = new date(date.getfullyear(), date.getmonth(), 1);   var firstdayofpreviousmonth = new date(date.getfullyear(), date.getmonth() - 1, 1)   var lastdayofpreviousmonth = new date(date.getfullyear(), date.getmonth(), 0)    if ($scope.myoptions.id === 1) { //show     startdate = null;   } else if ($scope.myoptions.id === 2) { //show 3 month     startdate = date.setdate(date.getdate() - 90);   } else if ($scope.myoptions.id === 3) { //show 6 month     startdate = date.setdate(date.getdate() - 180);   } else if ($scope.myoptions.id === 4) { //show previous month     startdate = date.parse(firstdayofpreviousmonth);     enddate = date.parse(lastdayofpreviousmonth);   } else { //default     startdate = date.parse(firstday)   }   $scope.selecteddatefilterrange(); }  $scope.selecteddatefilterrange = function() {   //filter data } 

but whenever im running code error appears

typeerror: cannot set property 'id' of undefined @ $scope.myoptions.id = 0;

you trying set property of undefined object: myoptions not exist.

change $scope.myoptions.id = 0;

to:

$scope.myoptions = { "id": 0 }; // or $scope.myoptions = {}; //    $scope.myoptions.id = 0; 

working demo


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -