小编And*_*rew的帖子

过滤掉select选项后,Angular模型无法更新

试图找出当绑定的选定选项不再存在时模型不更新的原因.我希望模型的属性更新为undefined/null/empty字符串.

情况:一个select驱动另一个select使用过滤器.选择完成后,转到原始select选项并选择其他选项.Filter将按select预期删除第二个选项,但第二个选项的model属性select将保持不变.

问题:当你去传递模型时,它将填充坏/先前的值.此外,使用Angular验证,select需要......表单在技术上是"有效的",因为模型具有属性的值(先前的值).

HTML:

<select name="Category" ng-model="selectedCategory" 
       ng-options="item.name as item.name for item in categories">
   <option value="">All Categories</option>
</select>

<select name="SubCategory" ng-model="selectedSubCategory" 
       ng-options="item.name as item.subCategory for item in categories | filter:selectedCategory">
  <option value="">All SubCategories</option>
</select>
Run Code Online (Sandbox Code Playgroud)

模型:

app.controller('MainCtrl', function($scope) {
   $scope.categories = [{
      "id": "1",
      "name": "Truck",
      "subCategory": "Telescope"
   }, {
      "id": "2",
      "name": "Truck",
      "subCategory": "Hazmat"
   }, {
      "id": "3",
      "name": "Van",
      "subCategory": "Mini"
   }];
});
Run Code Online (Sandbox Code Playgroud)

我相当肯定我可以将ng-change功能绑定到第一个以强制它更新模型,但是有更好的方法吗? …

javascript angularjs angularjs-ng-change angularjs-ng-options

6
推荐指数
1
解决办法
926
查看次数