0
我有以下形式:NG-禁用不與MD-選擇異步默認值工作
<form name="editUserForm" ng-cloak="">
<md-input-container layout-margin>
<label>Available Groups</label>
<md-select id="groups" ng-model="vm.selectedGroup" ng-model-options="{ trackBy: '$value.id' }" md-on-close="vm.clearGroupSearchTerm()"
data-md-container-class="selectdemoSelectHeader" multiple required>
<md-select-header class="demo-select-header">
<input id="searchGroup" ng-model="vm.groupSearchTerm" type="search" placeholder="Search available groups" class="demo-header-searchbox md-text"
ng-keydown="$event.stopPropagation()">
</md-select-header>
<md-optgroup label="Groups">
<md-option ng-value="group" ng-repeat="group in vm.groups |filter:vm.groupSearchTerm">{{group.name}}</md-option>
</md-optgroup>
</md-select>
</md-input-container>
<div layout="row" layout-align="end">
<md-button id="buttonCancel" class="md-raised md-primary" ui-sref="search.users" type="submit">Cancel</md-button>
<md-button id="buttonSave" class="md-raised md-primary" ng-disabled="editUserForm.$invalid" ng-click="vm.addOrUpdateUser()"
type="submit">Save</md-button>
</div>
</div>
</div>
</form>
選擇默認選項框異步加載:
/* @ngInject */
function UserController($scope, $state, $stateParams, $element,
usersService, groupsService, groupNameRegex, logger) {
var vm = this;
editUser()
function editUser() {
usersService.getUser($stateParams.userId).
then(function (data) {
vm.user = data;
for (var i = 0; i < data.userGroups.length; i++) {
vm.selectedGroup.push(data.userGroups[i].group);
}
// vm.selectedGroup = [{ id: 101, name: 'group1' }];
});
}
...
問題是當表單呈現時,即使模型「vm.selectedGroup」被正確加載並顯示,'Save'按鈕也被禁用。如果我硬編碼的結果,一切工作正常。 看起來'必需'屬性在返回承諾之前評估輸入選項。 我也讀過,異步調用的'required'屬性只在第一次選擇一個選項後才被評估。
所以我的問題是什麼是正確的方式來使用'$無效''所需'與默認值異步加載'md-select'元素。
如果我明白,當你設置'''''/ vm.selectedGroup = [{id:101,name:'group1'}];'它的作品,對吧? – lealceldeiro
這是正確的。 – Cyril