0

I am trying to uncheck checkbox with submit button. The idea is when checkbox is checked button is shown, and when button is clicked checkbox is unchecked and button is hidden.

HTML page:

<html ng-app="myApp" ng-controller="myCtrl">
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
  <script src="script.js"></script>
  <meta charset=utf-8 />
</head>

<body>
  <div ng-repeat="foo in boxes">
    <div>
      <input type="checkbox" name="cb" id="cb" ng-model="show" />{{foo}}
    </div>
    <div ng-show="show">
      <form ng-submit="submit()">
        <input type="submit" name="sumbit" id="sumbit" value="Hide" />
      </form>
    </div>
  </div>
</body>
</html>

JS:

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.boxes = ['a','b','c'];

  $scope.submit = function(){
    $scope.show = false;
  }
});

On Plukner: http://plnkr.co/edit/z9W0w18dkgYJ3D5Q3aR2?p=preview

Thanks for any help!

2
  • What exactly is your question? Can you explain where you're running into problems? Are you getting an error? Is anything happening? Commented Sep 24, 2014 at 21:26
  • How to uncheck checkbox with submit button? I set show on false in submit function but ng-model not recognize that show is false to uncheck checkbox. Am I on right way, or that functioning in other way? Commented Sep 24, 2014 at 21:35

2 Answers 2

3

The problem is that you're using a single variable to store states of 3 items yet Angular creates a scope for each context in the ng-repeat iteration. By changing show to an array and using $index to reference each of them, the show array from the main scope is passed to all three child scopes and there are no conflicts, so it works:

app.controller('myCtrl', function($scope) {
  $scope.boxes = ['a','b','c'];
  $scope.show = [];
  $scope.submit = function(){
    $scope.show = [];
  }
});

HTML

  <div ng-repeat="foo in boxes">
    <div>
      <input type="checkbox" name="cb" id="cb" ng-model="show[$index]" />{{foo}}
    </div>
    <div ng-show="show[$index]">
      <form ng-submit="submit()">
        <input type="submit" name="sumbit" id="sumbit" value="Hide" />
      </form>
    </div>
  </div>

See it here: http://plnkr.co/edit/kfTMaLTXWtpt7N9JVHAQ?p=preview

(note sure if this is exactly what you wanted because there's no question, but it's enough to get you started)


UPDATE

And here is the version where Hide unchecks only "its own" checkbox ($scope.submit now accepts the index parameter): http://plnkr.co/edit/YVICOmQrPeCCUKP2tBBl?p=preview

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

3 Comments

Yeah, that is what I exactly want. I thing in that way, but I didn't find out solution. Thanks a lot.
You're welcome! Yeah, it probably didn't like the fact you had 3 models double-binding to a single variable. Also, you have duplicate IDs on the page, make sure you fix that as well.
Yes, that was the main problem. I'll fix that.
0

You need to change the html code and simplify it as

Instead of form use simple ng-click

<div ng-show="show">
    <input type="submit" value="Hide" ng-click="show = !show" />
</div>

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.