1

I have the written that displays "selectable" images using checkboxes. I am trying to bind the selected images (encounter.secureImageUri) to an empty array in my controller using the following code. For some reason though it is not working. Can anyone tell me what I am doing wrong?

HTML:

<div ng-repeat="encounter in encounters.encounters" style="display: flex; flex-direction: row;">
     <img class="imgGallerySmall" alt="No capture taken"ng-src="{{encounter.secureImageUri}}" />
     <div>
          <p> 
              <input type="checkbox" ng-model="selectedImages" ng-change="updateSelection(encounter.secureImageUri)"/>
          </p>
     </div>
</div>

Controller:

$scope.selectedImages = [];

$scope.updateSelection = function (item) {
        $scope.selectedImages.push(item);
};

1 Answer 1

0

I don't see a full example to debug, but if you use ng-model here, it should point to a single variable rather than an array:

angular.module("exampleApp", [])
  .controller("imageSelectorCtrl", function($scope) {
    $scope.encounters = {
      encounters: [
        {
          secureImageUri: "https://picsum.photos/50/50",
          checked: false,
        },
        {
          secureImageUri: "https://picsum.photos/50/51",
          checked: false,
        },
      ]
    };

    $scope.selected = [];
    $scope.updateSelected = () => {
      $scope.selected = $scope
        .encounters
        .encounters
        .filter(e => e.checked);
    }
  });
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="color-scheme" content="dark light" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.js"></script>
</head>
<body>
  <div ng-app="exampleApp">
    <div ng-controller="imageSelectorCtrl">
      <div ng-repeat="encounter in encounters.encounters" style="display: flex;">
        <img alt="No capture taken" ng-src="{{encounter.secureImageUri}}" />
        <div>
          <p>
            <input type="checkbox" ng-model="encounter.checked" ng-change="updateSelected()" />
          </p>
        </div>
      </div>
      <div ng-repeat="img in selected">
        {{img.secureImageUri}}
      </div>
    </div>
  </div>
</body>

Or without ng-model, managing the array yourself based on clicks:

angular.module("exampleApp", [])
  .controller("imageSelectorCtrl", function($scope) {
    const ctrl = this;
    $scope.encounters = {
      encounters: [
        {secureImageUri: "https://picsum.photos/50/50"},
        {secureImageUri: "https://picsum.photos/50/51"},
      ]
    };
    $scope.selectedImages = [];

    $scope.updateSelection = function($event, item) {
      if ($event.target.checked && !$scope.selectedImages.includes(item)) {
        $scope.selectedImages.push(item);
      }
      else if (!$event.target.checked) {
        const i = $scope.selectedImages.indexOf(item);

        if (i >= 0) {
          $scope.selectedImages.splice(i, 1);
        }
      }
    };
  });
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="color-scheme" content="dark light" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.js"></script>
</head>
<body>
  <div ng-app="exampleApp">
    <div ng-controller="imageSelectorCtrl">
      <div ng-repeat="encounter in encounters.encounters" style="display: flex;">
        <img alt="No capture taken" ng-src="{{encounter.secureImageUri}}" />
        <div>
          <p>
            <input type="checkbox" ng-click="updateSelection($event, encounter.secureImageUri)" />
          </p>
        </div>
      </div>
      <div ng-repeat="img in selectedImages">
        {{img}}
      </div>
    </div>
  </div>
</body>

See also: How do I bind to list of checkbox values with AngularJS?

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

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.