1

I have this checkbox

          <input type="checkbox" 
          ng-change="defaultMsgsHandler(vuser.use_managed_default_msgs)" 
          ng-model="vuser.use_managed_default_msgs" 
          ng-true-value=1 ng-false-value=0>

When i make this ajax call, the checkbox does not reflect the ajax retured has vuser.use_managed_default_msgs set to 1, but the checkbox is not checked. I know the model is correct. vuser.use_managed_default_msgs is a 1 if i dump vuser to the console.

    messageServices.getMessageSettingsInfo(user.dir)
                    .then(function(data) {
                        if (!data.success) {
                            $scope.errormessage = data.errors;
                        } else {
                            $scope.vuser = data.vuser;
                            $scope.messages = data.messages;
                        }
                    }, function(error) {
                        alert(error);
                    });

If i click on the checkbox defaultMsgsHandler changed the model correctly, and the checkbox checks and unchecks.

    $scope.defaultMsgsHandler = function( use_managed_default_msgs ){
        $scope.vuser.use_managed_default_msgs = use_managed_default_msgs;
    };

Thanks for any help

3
  • Have you tried using $scope.$apply()? Commented Mar 13, 2015 at 3:15
  • 1
    Can we see an example of what data.vuser looks like when it is returned from the promise? I believe that is where the problem is. Commented Mar 13, 2015 at 3:28
  • Cannot reproduce ~ plnkr.co/edit/LqCD349a3DAnk4d4XgpY?p=preview Commented Mar 13, 2015 at 3:30

2 Answers 2

2

Please, try to use: ng-checked="condition"

There is a similar question here,

How to set checkbox selected on the basis of model value

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

Comments

0

Not sure if it will help, but I think your

ng-true-value=1 ng-false-value=0

should be

ng-true-value="1" ng-false-value="0"

4 Comments

I thought the same but it doesn't appear to make a difference.
unless your ajax response is returning a string not an integer, so it so would be ng-true-value="'1'"
Ah, great pickup. I just tried that and yes, if the value is a string it doesn't match the true value. Quoting the ng-true-value attribute values doesn't change that though
the string '' did the trick if in database the value is not an integer

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.