0

I have data in my controller that looks like this: $scope.myString = "00011" Is there a way I can model checkboxes to each character in the string, something like below?

<input type="checkbox" ng-model="myString.charAt(0)" ng-checked="'1'"> <input type="checkbox" ng-model="myString.charAt(1)" ng-checked="'1'"> <input type="checkbox" ng-model="myString.charAt(2)" ng-checked="'1'"> <input type="checkbox" ng-model="myString.charAt(3)" ng-checked="'1'">

End result, I'd like myString to hold a string of the 1's and 0's I've checked. My above example does not work but shows I have made an attempt to solve the problem.

2 Answers 2

2

you can do this , please refer below snippet.

function TodoCtrl($scope) {


$scope.myString = "00011";

$scope.checkBoxModel =  Array.from($scope.myString);


$scope.check = function(){
  $scope.myString = $scope.checkBoxModel.join("");
console.log( $scope.myString);
}

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="TodoCtrl">
   <input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()" ng-model="checkBoxModel[0]">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()"  ng-model="checkBoxModel[1]">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()" ng-model="checkBoxModel[2]">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()"  ng-model="checkBoxModel[3]">
 
  </div>
</div>

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

1 Comment

This worked great in my solution, Just a note, in my application I did not require the change() function As I just converted to a string on a submit to the server.
-1

We can not do this directly without an intermediate manipulations

        $scope.myString = "00111";


        $scope.booArr = [];

$scope.stringToValue($scope.myString);


        $scope.stringToValue = function(convertString){
            var len = convertString.length;
            for(var i=0; i<len; i++)
            {
                if($scope.myString.chatAt(i) == '0')
                {
                    $scope.booArr[i] = false;
                }
                else
                {
                    $scope.booArr[i] = true;
                }
            }
        };

        $scope.valueToString = function(){
            var len = $scope.booArr.length;

            var outputString = '';

            for(var i=0; i<len; i++) {
                if ($scope.booArr[i]) {
                    outputString = outputString + '1';
                }
                else {
                    outputString = outputString + '0';
                }
            }
            $scope.myString = outputString;

        };

Now in the HTML

<input type="checkbox" ng-repeat="item in boolArr" ng-model="item" ng-change="valueToString()">

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.