【发布时间】:2019-11-16 11:35:17
【问题描述】:
我有一个 angular-material md-select 元素,具有多项选择,我这样定义:
<md-select ng-model="selectedTypes"
ng-change="typesChanged()"
multiple
ng-disabled="availableTypes.length == 0">
<md-option ng-repeat="type in availableTypes" ng-value="{{type}}">
{{type.displayName}}
</md-option>
</md-select>
在控制器加载期间,我从服务器加载可用类型的数组,然后将selectedTypes 设置为availableTypes[0](在加载availableTypes 之后)如下:
$scope.promise = Types.query(query, function (types) { // Types is a $resource
$scope.availableTypes = types;
$scope.selectedTypes = [$scope.availableTypes[0]];
});
因为selectedTypes 是md-select 指令的模型,所以我预计它将使第一个选择选项成为选中状态。但它没有用。我在文档中找不到任何以编程方式选择 md-select 中的元素的方法。
【问题讨论】:
-
因此,您希望在页面加载时在
md-select中选择一些预定义的值。对吗? -
@nextt1 - 没错。但在某些情况下,由于其他状态更改(不仅是页面加载),我想以编程方式更改选择。所以我更喜欢找到一个不仅与页面加载相关的解决方案。
标签: angularjs angular-material