【发布时间】:2016-06-08 21:12:11
【问题描述】:
我正在使用一个数组来填充一个带有ng-options 的列表,以及一个绑定到所选项目的属性。清空数组后,绑定变量myObject.selectedItem保持最后选中项的值。
HTML:
<select ng-model="myObject.selectedItem" ng-options="item.Id as item.Name for item in myArrayList | orderBy:'Id'" required>
<option value="">Select something</option>
</select>
JS:
$scope.myArrayList = [ { Id: 1, Name: "Item 1" }, { Id: 2, Name: "Item 2" } ];
$scope.myObject = { selectedItem: null };
...选择项目2后:
$scope.myArrayList.length = 0; // Clears the array
console.log($scope.myObject.selectedItem); // Prints: 2
这是正常行为吗?
【问题讨论】:
标签: javascript angularjs angularjs-ng-model angularjs-ng-options