formly 动态选项中的默认值
formly Default value in dynamic options
我使用 angular js formly 来创建我的网络表单。
我的问题是当使用 select 和动态选项时默认值 属性 不起作用。但是当将静态数据设置为选项时它是有效的。
当我从远程服务器获取选项时,如何正式设置默认值(selected 选项)?
{
className: 'col-sm-3',
key: 'cityId',
type: 'select',
templateOptions: {
placeholder: 'City',
required: true,
label: 'City',
valueProp: 'id',
labelProp: 'name',
options: $scope.cityCombo
},
defaultValue: '3209C692-B8D4-4AB0-9F40-008A7C4644F9',
}
您可以为此字段使用 controller
选项。
这是根据当前产品类别动态获取 selection 选项的所有类别和 select 字段的设置值的示例:
{
key: 'category',
type: 'select',
wrapper: "loading",
templateOptions: {
label: 'Category',
options: [],
valueProp: 'name',
labelProp: 'name'
},
controller: function($scope, productsFactory, $routeParams) {
$scope.to.loading = productsFactory.getCategories().then(function(response){
$scope.to.options = response.data.categories;
// note, the line above is shorthand for:
// $scope.options.templateOptions.options = data;
productsFactory.getProduct($routeParams.id).then(function(response) {
$scope.model.category = response.data.product.category.name;
});
return response;
});
}
}
在你的情况下应该是这样的:
{
className: 'col-sm-3',
key: 'cityId',
type: 'select',
templateOptions: {
placeholder: 'City',
required: true,
label: 'City',
valueProp: 'id',
labelProp: 'name',
options: $scope.cityCombo
},
// defaultValue: '',
controller: function($scope /*inject*/) {
// some code
$scope.model.cityId = cityId; // dynamically set cityId
// some code
};
}
}
我使用 angular js formly 来创建我的网络表单。 我的问题是当使用 select 和动态选项时默认值 属性 不起作用。但是当将静态数据设置为选项时它是有效的。 当我从远程服务器获取选项时,如何正式设置默认值(selected 选项)?
{
className: 'col-sm-3',
key: 'cityId',
type: 'select',
templateOptions: {
placeholder: 'City',
required: true,
label: 'City',
valueProp: 'id',
labelProp: 'name',
options: $scope.cityCombo
},
defaultValue: '3209C692-B8D4-4AB0-9F40-008A7C4644F9',
}
您可以为此字段使用 controller
选项。
这是根据当前产品类别动态获取 selection 选项的所有类别和 select 字段的设置值的示例:
{
key: 'category',
type: 'select',
wrapper: "loading",
templateOptions: {
label: 'Category',
options: [],
valueProp: 'name',
labelProp: 'name'
},
controller: function($scope, productsFactory, $routeParams) {
$scope.to.loading = productsFactory.getCategories().then(function(response){
$scope.to.options = response.data.categories;
// note, the line above is shorthand for:
// $scope.options.templateOptions.options = data;
productsFactory.getProduct($routeParams.id).then(function(response) {
$scope.model.category = response.data.product.category.name;
});
return response;
});
}
}
在你的情况下应该是这样的:
{
className: 'col-sm-3',
key: 'cityId',
type: 'select',
templateOptions: {
placeholder: 'City',
required: true,
label: 'City',
valueProp: 'id',
labelProp: 'name',
options: $scope.cityCombo
},
// defaultValue: '',
controller: function($scope /*inject*/) {
// some code
$scope.model.cityId = cityId; // dynamically set cityId
// some code
};
}
}