Javascript 如何定义ng选项的选定值并将其分配给模型?
我有一个这样的数据,我编译它来选择选项,并在其中选择定义为Javascript 如何定义ng选项的选定值并将其分配给模型?,javascript,angularjs,model,ng-options,Javascript,Angularjs,Model,Ng Options,我有一个这样的数据,我编译它来选择选项,并在其中选择定义为defaultValue:1的值 content: [ { value: "Bireysel", key: "1", defaultValue: 0 }, { value: "Kurumsal", key: "2", defaultValue: 1 }, { value: "Bireyse
defaultValue:1的值
content: [
{
value: "Bireysel",
key: "1",
defaultValue: 0
},
{
value: "Kurumsal",
key: "2",
defaultValue: 1
},
{
value: "Bireysel & Kurumsal",
key: "3",
defaultValue: 0
}
]
我可以将其转换为选择选项,并将用户选择的值发送到服务器
<select ng-model="model.customer" ng-options="item.key as item.value for item in model.properties['CUSTOMER']" name="customer" required>
</select>
我还尝试了ng repeat
而不是ng选项
。这个方法看起来脏兮兮的,它选择了默认值true,但若用户并没有改变任何选项,我就不能将true数据发送到服务器。出现这种情况的原因是ng selected
未为我的型号指定默认值:model.customer
<select ng-model="model.customer" name="customer" required>
<option ng-repeat="item in model.properties['CUSTOMER']" value="{{item.key}}" ng-selected="item.defaultValue">{{item.value}}</option>
</select>
{{item.value}}
如何使用ng options
定义默认值,并将其分配给我的模型以将真实数据发送到服务器?可以使用ng init设置模型的默认值,如下所示:
<select ng-model="customer" ng-init="customer = content[0].key" ng-options="item.key as item.value for item in content" name="customer" required>
JSFIDLEvar CONTRIBUINTE=-1,
IMOVEL=-2,
ECONOMICO=-3,
$scope.tiposConsulta=[
{Id:contribute,descripcao:'contribute'},
{Id:IMOVEL,descripcao:'Imóvel'},
{Id:ECONOMICO,descripcao:'Econômico'}];
$scope.tiposConsultaSelected=contribute;
我需要根据defaultValue定义所选值,如果为1,则应选择该值。非常感谢您+我的1。最后一个问题是,如何将此函数转换为DynMac,以便在多脚本选择框上使用,示例如下:
<select ng-model="customer" ng-init="customer = content[0].key" ng-options="item.key as item.value for item in content" name="customer" required>
var CONTRIBUINTE = -1,
IMOVEL = -2,
ECONOMICO = -3,
$scope.tiposConsulta = [
{ Id: CONTRIBUINTE, Descricao: 'Contribuinte' },
{ Id: IMOVEL, Descricao: 'Imóvel' },
{ Id: ECONOMICO, Descricao: 'Econômico' }];
$scope.tiposConsultaSelected = CONTRIBUINTE;
<select id="tipoConsulta" class="btn form-control"
ng-model="tiposConsultaSelected"
ng-options="tipo.Id as tipo.Descricao for tipo in tiposConsulta">
</select>