最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unchecking a checkbox with angularjs - Stack Overflow

programmeradmin1浏览0评论

I have a model that renders a list of checkboxes. When I check one box I would like any other checked boxes to bee unchecked. The below code seems to iterate fine, but the html is not reflecting the changes. What am I missing?

HTML:

<tr ng-repeat="x in monOrders">
                            <td>{{x.ProductName}}</td>
                            <td>{{x.ProductItemCode}}</td>
                            <td>{{x.ProductSize}}</td>
                            <td><input type="checkbox" 
                                       ng-click="updateCommonProduct(x)" 
                                       ng-checked="x.IsChecked"/></td>
                        </tr>

angular/javascript:

$scope.updateCommonProduct = function(product) {

    // uncheck non-selected mon orders
    angular.forEach($scopemonOrders, function(value, key) {
        if (value.ProductItemCode !== product.ProductItemCode) {
            value.IsChecked = false;
        }
    }, $scopemonOrders);



};

I have a model that renders a list of checkboxes. When I check one box I would like any other checked boxes to bee unchecked. The below code seems to iterate fine, but the html is not reflecting the changes. What am I missing?

HTML:

<tr ng-repeat="x in monOrders">
                            <td>{{x.ProductName}}</td>
                            <td>{{x.ProductItemCode}}</td>
                            <td>{{x.ProductSize}}</td>
                            <td><input type="checkbox" 
                                       ng-click="updateCommonProduct(x)" 
                                       ng-checked="x.IsChecked"/></td>
                        </tr>

angular/javascript:

$scope.updateCommonProduct = function(product) {

    // uncheck non-selected mon orders
    angular.forEach($scope.monOrders, function(value, key) {
        if (value.ProductItemCode !== product.ProductItemCode) {
            value.IsChecked = false;
        }
    }, $scope.monOrders);



};
Share Improve this question asked Aug 24, 2014 at 20:28 user1206480user1206480 1,8586 gold badges29 silver badges46 bronze badges 2
  • 3 Can you just use radio buttons? – bencripps Commented Aug 24, 2014 at 20:31
  • can you post a Plunkr/JsFiddle ? :) – meriadec Commented Aug 24, 2014 at 20:36
Add a ment  | 

1 Answer 1

Reset to default 4

You should use ngModel & ngChange:

  • ngModel creates a two-way binding between the model and the DOM.
  • ngChange triggers an event when a model gets change.

Here is a plunker: http://plnkr.co/edit/lrQnA6JrvlsirqK8QGaE?p=preview

Template:

<tr ng-repeat="x in monOrders">
   <td>{{x.ProductName}}</td>
   <td>{{x.ProductItemCode}}</td>
   <td>{{x.ProductSize}}</td>
   <td>
     <input type="checkbox" ng-change="check(x)" ng-model="x.isChecked"/>
   </td>
 </tr>

Controller method:

$scope.check = function(current) {
  angular.forEach($scope.monOrders, function(product) {
    if(product !== current) {
      product.isChecked = false;  
    }
  });
};
发布评论

评论列表(0)

  1. 暂无评论