3

I have a simple form with radio buttons that pulls values from a server and loops through them to provide radio buttons:

<form ng-submit="createSubCategory(subcategory)">
    <div ng-repeat="sub_category in event_sub_categories" >
        <label class="item item-radio">
                  <input type="radio" ng-model="subcategory" ng-value="'{{sub_category}}'" >
                  <div class="radio-content">
                    <div class="item-content">
                      {{sub_category}}
                    </div>
                    <i class="radio-icon ion-checkmark"></i>
                  </div>
         </label>
    </div>
    <div class="padding">
        <button type="submit" class="button button-block button-positive">Continue</button>
    </div>
</form>

The form displays perfectly. However, the radio button that I press doesn't seem to be saving. Here is the createSubCategory method:

$scope.createSubCategory = function(subcategory){
  console.log(subcategory);
}

The logs are showing undefined. How do I get the subCategory to be logged after filling out the form?

4

1 回答 1

7

ng-repeat 创建自己的范围。由于您将单选按钮绑定到subcategory,因此填充的是 ng-repeat 范围的subcategory字段,而不是控制器范围的subcategory字段。

经验法则:

  • 在你的 ng-model 中总是有一个点
  • 始终创建包含您要绑定的字段的对象

此外, ng-value 需要一个角度表达式。

所以,在你的控制器中,有这个:

$scope.formModel = {};

在视图中:

<input type="radio" ng-model="formModel.subcategory" ng-value="sub_category" >
于 2016-04-19T20:45:35.367 回答