1

I want to store value from checkbox into string

 <label style="color:black; text-decoration:none; font-size:15px" href="#">
                  <input type="checkbox" ng-model="diet.v1" ng-true-value="'&diet=high-fiber'" ng-false-value="''" />High-Fiber</label> 

How do i get value from ng-true-value (when it's checked) to string so i can use it ? when i try something like this console.log(diet.v1); in js it wont work

1 Answer 1

1

Please check the following working sample code with ng-true-value, ng-false-value.

 <!doctype html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <title>Example - example-checkbox-input-directive-production</title>
          
        
          <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
          
        
          
        </head>
        <body ng-app="checkboxExample">
          <script>
          angular.module('checkboxExample', [])
            .controller('ExampleController', ['$scope', function($scope) {
              $scope.diet = {
               v1 : ''
             };
            }]);
        </script>
        <form name="myForm" ng-controller="ExampleController">
          <label>Value:
            <input type="checkbox" ng-model="diet.v1"
                   ng-true-value="'&diet=high-fiber'" ng-false-value="''">
           </label><br/>
          <tt>value = {{diet.v1}}</tt><br/>
         </form>
        </body>
        </html>

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

5 Comments

this doesent change value of .value1 between what i need and empty string when i click checkbox
value1 is just for reference. checkboxModel.value2 is based on your requirement.
i know that but when i put my own costum value and try clicking checkbox, that value doesent change between ng-true-value and ng-false-value for some reason
Please check the plunker, I have used same like your code: plnkr.co/edit/GIWTaXpuTUIaMX9aXx6L
The code snippet also added, please check. You can also post your code in plunker for further check.

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.