更新单选按钮选择单击Angular中的父元素

时间:2015-02-27 16:14:01

标签: angularjs angular-ui

假设这个模型:

      <li ng-repeat="item in documentViews" for ={{item}}>
        <div class="col-sm-12 document-view-item" ng-click="change($index)">
          <div class="row">
            <img class="document-view-img" src="../assets/images/kml_document.png">
          </div>

          <div class="row">
              <input type="radio" ng-model="prefs.documentView" ng-value="item.value" id="{{item.value}}" name="documentView" ng-selected="prefs.documentView">
              {{item.key}}
                </input>
          </div>
        </div>
      </li>

这个控制器:

angular.module('app')
  .controller('DocumentViewCtrl', function ($scope) {

    $scope.subtitle = 'Default document view';
    $scope.documentViews = [
      {
        key: 'Blah1',
        value: 'b1'
      },
      {
        key: 'Blah2',
        value: 'b2'
      },
    ];

    $scope.change = function($index) {
      console.log($scope.documentViews[$index].value);
    };
});

我不想仅在选择输入标签时更新单选按钮,而是在单击出现ng-click的更高div元素时更新单选按钮。当发生点击时,我无法触发控制台日志,这很好,但我想知道如何更新单选按钮上的选择,从而更新$ scope。

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

在你的HTML中:

  <li ng-repeat="item in documentViews" for="{{item}}">
    <div class="col-sm-12 document-view-item" ng-click="change(item)">
      <div class="row">
        <img class="document-view-img" src="../assets/images/kml_document.png">
      </div>

      <div class="row">
          <input type="radio" ng-model="prefs.documentView" ng-value="item.value" id="{{item.value}}" name="documentView" ng-selected="prefs.documentView">
          {{item.key}}
            </input>
      </div>
    </div>
  </li>

在您的控制器中:

$scope.change = function(item) {
  $scope.prefs.documentView = item.value;
}