0

I have this HTML:

<div>
    <input type="checkbox" ng-model="EmailToUser" />
    <input type="checkbox" ng-model="EmailToOwner" />
</div>

And this in my controller.js:

$scope.EmailToUser = true;
$scope.EmailToOwner = false;

$scope.Save = function() {
    if($scope.EmailToUser) {
        alert("I'm supposed to email the user.");
    }

    if($scope.EmailToOwner) {
        alert("I'm supposed to email the owner.");
    }
}

This doesn't work, when I click the checkbox the values true/false are constant for some reason. EmailToUser is always true and EmailToOwner is always false regardless of the checkbox state.

But, if I change the code to this:

<div>
    <input type="checkbox" ng-model="EmailToUser.Value" />
    <input type="checkbox" ng-model="EmailToOwner.Value" />
</div>

And controller.js:

$scope.EmailToUser = {};
$scope.EmailToUser.Value = true;
$scope.EmailToOwner = {};
$scope.EmailToOwner.Value = false;

$scope.Save = function() {
    if($scope.EmailToUser.Value == true) {
        alert("I'm supposed to email the user.");
    }

    if($scope.EmailToOwner.Value == true) {
        alert("I'm supposed to email the owner.");
    }
}

It works. Why? I can't seem to figure the differences between #1 and #2. Am I not creating new objects the same way inside the scope and assigning a true/false value in both ways?

4
  • 3
    Is this inside an ng-repeat, ng-switch etc.. or something? youtube.com/watch?v=ZhfUv0spHCY&feature=youtu.be&t=30m Commented Jul 28, 2014 at 17:21
  • 1
    can you setup a plunker showing the problem... Commented Jul 28, 2014 at 17:32
  • @PSL I believe not, it's a modal, so the code is closed inside the window (I think so). If not, there's no repeat or switch, but there might have a layer or two above it... (I'm using $scope though) Commented Jul 28, 2014 at 18:11
  • 1
    github.com/angular/angular.js/wiki/Understanding-Scopes Commented Jul 28, 2014 at 18:55

1 Answer 1

0

controller.js(code)

$scope.EmailToUser = true;
$scope.EmailToOwner = false;
$scope.save1 = function(EmailToUser,EmailToOwner){

    if($scope.EmailToUser) {
        alert("I'm supposed to email the user.");
    }if($scope.EmailToOwner) {
        alert("I'm supposed to email the owner.");
    }
}; 

controller.html

<div>
    <input type="checkbox" ng-model="EmailToUser" />
    <input type="checkbox" ng-model="EmailToOwner" />
</div>
<button  type="button" ng-click="save1(EmailToUser,EmailToOwner)">click</button>

This code works for me.There is some scope issue.

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.