Javascript 动态选项中的格式默认值
我使用angular js formly创建web表单。 我的问题是,当将选择与动态选项一起使用时,defaultValue属性不起作用。但当将静态数据设置为选项时,它是有效的。 从远程服务器获取选项时,如何在formly中设置默认值(选定选项)Javascript 动态选项中的格式默认值,javascript,angularjs,angular-formly,Javascript,Angularjs,Angular Formly,我使用angular js formly创建web表单。 我的问题是,当将选择与动态选项一起使用时,defaultValue属性不起作用。但当将静态数据设置为选项时,它是有效的。 从远程服务器获取选项时,如何在formly中设置默认值(选定选项) { className: 'col-sm-3', key: 'cityId', type: 'select', templateOptions: { placeholder: 'City', required: true, la
{
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',
}您可以对该字段使用
控制器
选项
这是根据当前产品的类别动态获取选择选项的所有类别和设置选择字段值的示例:
{
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
};
}
}