Issue
I have a dropdown list. MY objective is to set the dropdown list to "default" on load
<b>Filter Within Months:</b>
<select class="btn green" ng-model="myOptions" ng-options="i.label for i in items track by i.id" ng-change="selectedItemChanged()">
</select>
controller.js setting the model to "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 All
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 the data
}
but whenever im running the code an error appears
TypeError: Cannot set property 'id' of undefined at $scope.myOptions.id = 0;
Solution
You are trying to set a property of an undefined object: myOptions does not exist.
Change $scope.myOptions.id = 0;
To:
$scope.myOptions = { "id": 0 };
// Or $scope.myOptions = {};
// $scope.myOptions.id = 0;
Working demo
Answered By - Mistalis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.