如何接受选项框上的值

时间:2017-06-28 02:26:56

标签: javascript html angularjs

我的选项中有一个ng-model列表数组。每个具有不同值的ng模型分配取决于用户输入。当我尝试选择选项框并且结果产生了我想要的内容时,它对我有用。但是当我从API检索新数据并将值解析到该选项框时。不工作。

HTML

<div ng-repeat="($a_choice_index, c) in menu.additionalchoice">
  <label for="max_select" class="rl-label required">Do you want maximum select?
    <label for="max_select" class="select-block">
      <select id="max_select" name="max_select" ng-model="c[$a_choice_index].maxSelect" ng-change="selectMaxSelect(c[$a_choice_index].maxSelect, $a_choice_index)" ng-options="o.name for o in maxSelectOptions"></select>
      <svg class="svg-arrow">
        <use xlink:href="#svg-arrow"></use>
      </svg>
    </label>
  </label>
</div>

控制器

$scope.menu = {};
$scope.maxSelectOptions = [
 {
    name: "No",
    value: 0
 }, 
 {
    name: "Yes",
    value: 1
 }
];

$scope.selectMaxSelect = function(v, index){
  $scope.menu.mealchoices[index].max_select = v;
};

MyService.GetData(function(menu){
    $scope.menu = menu; // menu.additionalchoice is added into menu
});

API数据

每个menu.additionalchoice产生

  

{&#34; 1&#34;:{&#34; title&#34;:&#34;添加最喜欢的油&#34;,&#34; maxSelect&#34;:{&#34; name&# 34;:&#34;否&#34;,&#34;价值&#34;:0}},&#34;标题&#34;:&#34;添加喜爱的油&#34;,&#34; max_select& #34:4,&#34; additional_choice&#34;:1,&#34; maxSelect&#34;:1,&#34;膳食&#34;:...

当我尝试:

1)ng-init="c[$a_choice_index].maxSelect = c.maxSelect"

显示为空。

2)ng-init="selectMaxSelect(c.maxSelect, $a_choice_index)"

它显示了一些值,但值不正确。当c[$a_choice_index].maxSelect为YES时,{"name": "No", "value": 0}会返回c.maxSelect == 1

1 个答案:

答案 0 :(得分:0)

您只能使用... as ... for ... in ...语法按值绑定select选项,与使用object绑定相比,这更容易。

ng-options="o.value as o.name for o in maxSelectOptions"

ng-model中的结果现在应该是0或1而不是对象。