0

I would like to set some items in list as checked, but under some condition.

<div data-ng-repeat="item in items>
     <input type="checkbox" checked==item.selected> {{item.name}}
</div>

Let's imagine that I have some fileld an array named items where every single item in it has defined property selected. Depending on value of it input checkbox should be checked or not.

3
  • 1
    shouldnt it be checked={{item.selected}} ? Commented Sep 7, 2015 at 13:44
  • stackoverflow.com/questions/15696416/… Commented Sep 7, 2015 at 13:50
  • I am getting an error : cannot resolve tag -' {{condition.expanded}} ' Commented Sep 7, 2015 at 13:56

2 Answers 2

1

Try the below code:

<div data-ng-repeat="item in items>
     <input type="checkbox" ng-model="item.selected"> {{item.name}}
</div>

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This works, only change is ng-model="{{ item.selected }}"
0

Try this:

You can put your logic of conditions in ng-init as mentioned in example below:

angular.module('myApp', [])
  .controller('myController', function($scope) {
    $scope.items = [{
      name: 'A'
    }, {
      name: 'B'
    }, {
      name: 'C'
    }];
    $scope.youCondition = function(item) {
      //Your logic
      if (item.name === 'A' || item.name === 'C') {
        item.selected = false;
      }
      else
      {
        item.selected = true;
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>
  <meta charset="UTF-8">
  <title></title>
</head>

<body ng-controller="myController">
  <div data-ng-repeat="item in items">
    <input type="checkbox" ng-init="youCondition(item)" ng-model="item.selected">{{item.name}}
  </div>
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.