Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何将ng模型绑定到;“价值”;为用户界面选择所选项目的名称_Angularjs - Fatal编程技术网

Angularjs 如何将ng模型绑定到;“价值”;为用户界面选择所选项目的名称

Angularjs 如何将ng模型绑定到;“价值”;为用户界面选择所选项目的名称,angularjs,Angularjs,请查看以下链接: {{$select.selected.label} 我正在尝试让ui select将fm.countryCode设置为所选项目的值。目前,它只是将fm.countryCode设置为整个国家/地区项目。例如,如果我选择阿富汗,fm.countryCode将设置为{“value”:“AF”,“label”:“Afghanistan”}。我想要的是“AF” 我需要设置$watch才能实现这一点,还是有更优雅的解决方案?只需更改即可 fm.countryCode = {{fm.c

请查看以下链接:


{{$select.selected.label}
我正在尝试让ui select将fm.countryCode设置为所选项目的值。目前,它只是将fm.countryCode设置为整个国家/地区项目。例如,如果我选择阿富汗,fm.countryCode将设置为{“value”:“AF”,“label”:“Afghanistan”}。我想要的是“AF”

我需要设置$watch才能实现这一点,还是有更优雅的解决方案?

只需更改即可

fm.countryCode = {{fm.countryCode}}
为了

换衣服

fm.countryCode = {{fm.countryCode}}
为了

好的,替换

 fm.countryCode = {{fm.countryCode}}

请参见plunker中的“确定更换”

 fm.countryCode = {{fm.countryCode}}


请参阅plunker中的IMO更优雅的解决方案是:

更改此选项:

<ui-select ng-model="fm.countryCode" id="countryCode">
     <ui-select-match placeholder="Select a country...">{{$select.selected.label}}</ui-select-match>
     <ui-select-choices repeat="item in countries | filter: $select.search" value="{{$select.selected.value}}">
              <div ng-bind-html="item.label | highlight: $select.search"></div>
              <small ng-bind-html="item.value | highlight: $select.search"></small>
     </ui-select-choices>
 </ui-select>

{{$select.selected.label}
为此:

<select ng-options="country.value as country.label for country in countries" ng-model="fm.countryCode"> </select>


IMO更优雅的解决方案是:

更改此选项:

<ui-select ng-model="fm.countryCode" id="countryCode">
     <ui-select-match placeholder="Select a country...">{{$select.selected.label}}</ui-select-match>
     <ui-select-choices repeat="item in countries | filter: $select.search" value="{{$select.selected.value}}">
              <div ng-bind-html="item.label | highlight: $select.search"></div>
              <small ng-bind-html="item.value | highlight: $select.search"></small>
     </ui-select-choices>
 </ui-select>

{{$select.selected.label}
为此:

<select ng-options="country.value as country.label for country in countries" ng-model="fm.countryCode"> </select>


您可以将ui选择更改为:

<ui-select ng-model="fm.countryCode" id="countryCode">
  <ui-select-match placeholder="Select a country...">{{$select.selected.label}}</ui-select-match>
  <ui-select-choices repeat="item.value as item in countries | filter: $select.search" value="{{$select.selected.value}}">
      <div ng-bind-html="item.label | highlight: $select.search"></div>
      <small ng-bind-html="item.value | highlight: $select.search"></small>
  </ui-select-choices>
</ui-select>  

{{$select.selected.label}
这将以item.value作为完整项,而不是value和label


因此,重复部分:
您可以将ui选择更改为:

<ui-select ng-model="fm.countryCode" id="countryCode">
  <ui-select-match placeholder="Select a country...">{{$select.selected.label}}</ui-select-match>
  <ui-select-choices repeat="item.value as item in countries | filter: $select.search" value="{{$select.selected.value}}">
      <div ng-bind-html="item.label | highlight: $select.search"></div>
      <small ng-bind-html="item.value | highlight: $select.search"></small>
  </ui-select-choices>
</ui-select>  

{{$select.selected.label}
这将以item.value作为完整项,而不是value和label


所以重复部分:
看一看看我不认为他的最终目标是在他的html上显示fm.countryCode的值。我不认为他的最终目标是在他的html上显示fm.countryCode的值。