单击图像以检查radiobox

时间:2014-08-11 20:27:47

标签: javascript angularjs angularjs-directive ng-options

问题

点击图片或复选框后,如何显示相应的下拉列表。 最后将隐藏radiobox。

目前单选按钮确实已经过检查,但它确实使选项显示正确吗?我认为这是一个范围问题。

live code

我有一系列颜色和尺寸:

app.controller('Ctrl', function ($scope, $filter, $http) {
 $scope.productData = {
  "colors_and_sizes": {
    "data": {
      "Black": {
        "swatch_image": 'http://lorempixel.com/50/50',
        "sizes": "X Small, Small, Medium, Large, Xlarge, XX Large"
      },
      "Blue": {
        "swatch_image": 'http://lorempixel.com/50/50',
        "sizes": "X Small, Small, Medium, Large, Xlarge, XX Large"
      }
    }
  }
};

});

HTML:这是我的表格

<form ng-app="app" ng-controller="Ctrl">
    <div class="color-pick" ng-repeat="(key, val) in productData.colors_and_sizes.data">
        <input type="radio" ng-checked="selected" name="colors" ng-model="$parent.myColor" ng-value="key" />
        <img ng-click="selected = true" ng-src="{{val.swatch_image}}" alt="">{{key}} 
        <div class="size-pick" ng-show="$parent.myColor==key">
            <select ng-model="$parent.mySize" ng-options="size for size in val.sizes.split(',')"></select>
        </div>
    </div>
    myColor: {{myColor}}<br/>
    mySize: {{mySize}}
</form>

1 个答案:

答案 0 :(得分:1)

目前,您定义了$parent.myColor以保存单选按钮的值。因此,如果您希望能够根据所选图像显示下拉列表,则可以像现在一样在img标记上使用ng-click,并为其指定新值。

<form ng-app="app" ng-controller="Ctrl">
    <div class="color-pick" ng-repeat="(key, val) in productData.colors_and_sizes.data">
        <input type="radio" name="colors" ng-model="$parent.myColor" ng-value="key" />
        <img ng-click="$parent.myColor = key" ng-src="{{val.swatch_image}}" alt="">{{key}} 
        <div class="size-pick" ng-show="$parent.myColor==key">
            <select ng-model="$parent.mySize" ng-options="size for size in val.sizes.split(',')"></select>
        </div>
    </div>
    myColor: {{myColor}}<br/>
    mySize: {{mySize}}
</form>

Try it yourself