在下拉列表中设置默认值。

时间:2022-10-17 14:12:59

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.

控制器。js将模型设置为“默认”加载。

$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;

类型错误:无法在$scope.myOptions中设置未定义的属性“id”。id = 0;

2 个解决方案

#1


2  

You are trying to set a property of an undefined object: myOptions does not exist.

您试图设置一个未定义对象的属性:myOptions不存在。

Change $scope.myOptions.id = 0;

改变scope.myOptions美元。id = 0;

To:

:

$scope.myOptions = { "id": 0 };
// Or $scope.myOptions = {};
//    $scope.myOptions.id = 0;

Working demo

#2


-1  

Try this

试试这个

var index = _.findIndex($scope.items,{id:0});
$scope.myOptions = $scope.items[index];

Note: The first line requires underscore.js but you can replace it with pure javascript to get the index of the item with id = 0 from $scope.items

注意:第一行需要下划线。但是您可以用纯javascript替换它,以从$scope.items中获得id = 0的项的索引。

#1


2  

You are trying to set a property of an undefined object: myOptions does not exist.

您试图设置一个未定义对象的属性:myOptions不存在。

Change $scope.myOptions.id = 0;

改变scope.myOptions美元。id = 0;

To:

:

$scope.myOptions = { "id": 0 };
// Or $scope.myOptions = {};
//    $scope.myOptions.id = 0;

Working demo

#2


-1  

Try this

试试这个

var index = _.findIndex($scope.items,{id:0});
$scope.myOptions = $scope.items[index];

Note: The first line requires underscore.js but you can replace it with pure javascript to get the index of the item with id = 0 from $scope.items

注意:第一行需要下划线。但是您可以用纯javascript替换它,以从$scope.items中获得id = 0的项的索引。