1

I am trying to print text on the page in a <h1> tag depending on whether a check box is checked or not.

$scope.EmailMe = function(email) {
    if($scope.emailchecked == 1) {
        $scope.test = "emailSent";
    } else {
        $scope.test = "nothing";
    }
    }

HTML is :

        <input type="checkbox" ng-model="emailchecked" ng-change="EmailMe(1)">
        <h1> @{{test}} </h1>

I have the text printing but the checkbox is not being checked. or allowing for change please help :)

3
  • 1
    do you want to have 0 & 1 value in the checkbox? Commented Apr 14, 2016 at 2:12
  • @NishanthMatha the checkbox can now be checked :). But unfortunately the text is no longer printing. I know my code is messy im new to angular, is there a better approach to check if something is checked? Commented Apr 14, 2016 at 2:19
  • @PankajParkar I know that may be missing but I'm unsure of how it should be there Commented Apr 14, 2016 at 2:25

3 Answers 3

1

Use ng-true-value & ng-false-value, so that will give you 1/0 value based on selection.

<input type="checkbox" ng-model="emailchecked"
  ng-true-value="1" 
  ng-false-value="0" 
  ng-change="EmailMe(emailchecked)">

Demo Plunkr

Even if you don't use ng-true-value, but that would not kept you model value to 0/1. By default checkbox value is true/false.

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

1 Comment

Thank you alot, + 1 for teaching me inbuilt angularJS function that I did not know
1

HTML

  <body ng-controller="MainCtrl">
    <input type="checkbox" ng-model="emailchecked" ng-change="EmailMe()">
    <h1> @{{test}} </h1>
  </body>

Controller

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.EmailMe = function() {
    if($scope.emailchecked == 1) {
        $scope.test = "emailSent";
    } else {
        $scope.test = "nothing";
    }

    };

});

http://plnkr.co/edit/EfIMMn7Be7QL258aUiXW

and in your code you are changing the emailchecked which is not required.

Comments

0

You need to change if(email = 1) to if(email == 1). One equal is missing

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.