1

I have the following code in my application:

      <input type="checkbox" ng-checked="vm.eduToEdit.test" />
      {{vm.eduToEdit.test}}
      <input type="checkbox" ng-model="vm.eduToEdit.test">

the value vm.eduToEdit.test is diplaying true or false, but no matter what it returns the checkboes in either input above are not checked. I'm not sure what i'm missing, i thought i shouldn't use ng-model so then I tried ng-checked, but neither has worked so far.

EDIT: I tried the below, but same result:

       <input type="checkbox" ng-checked="vm.eduToEdit.test" /> - 
       <input type="checkbox" ng-checked="checking" /> - 
       {{vm.eduToEdit.test}}
       {{checking}}

In my controller i have:

    vm.eduToEdit.test = true;
    $scope.checking = true;

When I view the page, true is written out, but i never see it checked.

Below is my controller:

(function () {
'use strict';

myModule.controller('EducationController', ["$scope", "bootstrappedData", EducationController]);

function EducationController($scope, bootstrappedData) {
    var vm = this;
    vm.shoppingCart = bootstrappedData.shoppingCart;
    vm.eduToEdit = {};

    vm.EditEducation = function (applicantEducationId, educationTypeId) {
        var owl = $(".owl-carousel");

        var eduFound = $.grep(this.shoppingCart, function (h) {
            return h.ApplicantEducationId === applicantEducationId;
        });
        vm.eduToEdit = eduFound[0];
        vm.eduToEdit.formattedAttendEnd = vm.ConvertToDate(vm.eduToEdit.AttendEnd);
        vm.eduToEdit.formattedAttendStart = vm.ConvertToDate(vm.eduToEdit.AttendStart);
        vm.eduToEdit.formattedGraduationDate = vm.ConvertToDate(vm.eduToEdit.GraduationDate);

        vm.eduToEdit.test = true;
        $scope.checking = true;

        switch (educationTypeId) {
            case 1:
            case 2:
            case 3:
            case 4:
                owl.trigger('to.owl.carousel', [1, 50]);
                break;
            case 5:
                owl.trigger('to.owl.carousel', [3, 50]);
                break;
            case 6:
                owl.trigger('to.owl.carousel', [2, 50]);
                break;
            case 7:
                owl.trigger('to.owl.carousel', [4, 50]);
                break;
            case 9:
                owl.trigger('to.owl.carousel', [5, 50]);
                break;
        }
    };

    vm.ConvertToDate = function (jsonDateToConvert) {
        if (jsonDateToConvert == null)
            return "";
        var value = new Date
        (
             parseInt(jsonDateToConvert.replace(/(^.*\()|([+-].*$)/g, ''))
        );
        return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear();
    }
}

})();

9
  • Take a look at the documentation docs.angularjs.org/api/ng/directive/ngChecked) Note that this directive should not be used together with ngModel, as this can lead to unexpected behavior. Commented Apr 11, 2016 at 17:52
  • @AlonEitan the 1st example doesn't use ng-model, shouldn't that work? As the value of test is always true, that comes from the database, i don't understand why the 1st one doesn't get checked then. Commented Apr 11, 2016 at 18:13
  • 1
    any error shown ? can you share your controller code? Commented Apr 11, 2016 at 18:20
  • Don't think it wok that way, maybe ng-click="vm.eduToEdit.test = !vm.eduToEdit.test" instead of ng-checked="vm.eduToEdit.test" will work Commented Apr 11, 2016 at 18:21
  • is .test a getter function? Commented Apr 11, 2016 at 18:31

2 Answers 2

1

When you will use ng-checked then it will act as one-way binding means only show checked or unchecked this check box but not changed in controller variable. If you want to change controller variable on change in dom then better to use ng-model to checked or unchecked. can try like:

I guess you used controller as vm in dom and used this in controller.

in html:

<body ng-controller="myCtrl as vm">
    <input type="checkbox" ng-model="vm.eduToEdit.test">{{vm.eduToEdit.test}}
</body>

controller:

angular.module('myApp',[])
.controller("myCtrl", function($scope) {
  var vm = this;
  vm.eduToEdit = {};
  vm.eduToEdit.test = true;
});

DEMO LINK

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

5 Comments

Please see my update, i have something similar to what you have, i see it running in your demo, not sure what i'm missing though.
can you share more controller code and html with ng-controller element ?
are you using ng-controller="yourCtrl as vm" ? and some time it happened for digest problem. try to assign in $timeout function for vm.eduToEdit.test = true/false;
yes, i'm using ng-controller="EducationController as vm" i tried putting the checkbox right underneath the div with the ng-controller, thinking maybe it's out of scope, but same issue. i intially thought maybe the true false was coming back as a string, so tried to manually set it, but no luck.
I believe the reason that it's not working might have to do with Bootstrap, we are using a bootstrap switch and it looks like it does not play well with Angular as it requires Jquery, so will look into the angular-bootstrap project
0

Click on the second check box to update first, since ng-model is bind with second checkbox whenever you click on it the model value in first checkbox gets updated.

    <input type="checkbox" ng-checked="vm.test" />
    {{vm.test}}
    <input type="checkbox" ng-model="vm.test" /> 

jsfiddle

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.