Good day! Tell me, please, how can I implement the execution of some action in a function in the controller with the checked / unchecked checkbox? Say, when you press the button, the button is ng-click="имя_вызываемой_функции(передаваемые_параметры)" , and in the controller the function itself, and how to do the same for the checkbox? Moreover, to transfer its status is set / not set, or, depending on the state, to call two different functions.

And how to set the initial value of the checkbox?

    2 answers 2

    Not a very high-quality solution, but if you don’t want to create functions in the controller, then try this:

     <input type="checkbox" ng-true-value="YES" ng-false-value="NO" ng-model="mycheckbox" ng-change="mycheckbox=='YES' ? first() : other()" /> 
    • Thanks, but how to set the initial value based on the result of a function? - Emm
    • Just assign the value of the mycheckbox model (in fact, it’s better to have one object for the model, that is, model.mycheckboxModel ). Then in $scope it will be possible to refer to the value of the model. - Cyrus
    • thank! Tell me, please, and if there is a set of checkboxes on the page, how can I set a specific checkbox to a specific value? I now have every checkbox with ng-model = "myModel", in the controller I set the value of the checkbox via $ scope.myModel = value, the value is set, but for all checkboxes at once, since they have one model. And how can they be identified individually? - Emm
    • @Emm, do you add the checkbox manually? Then you can specify different models manually, or else let the model be an array and you can refer to it by index. Or, use ng-repeat on the model with an array of value elements. - Cyrus
    • In the controller: checkboxes = [ { name: "Первый чекбокс", cbState: false } , {"name": "Второй чекбокс", cbState: true } ] In the markup: <span ng-repeat="cb in checkboxes"><input type="checkbox" ng-model="cb.cbState" /> {{cb.name}}</span> - Cyrus
     <input type=checkbox ng-model=smth ng-change=doSmth(smth)> 
    • Thanks, but how to set the initial value based on the result of a function? - Emm
    • @Emm, in the controller $scope.smth = someFunc(); . - Qwertiy
    • thank! Tell me, please, and if there is a set of checkboxes on the page, how can I set a specific checkbox to a specific value? I now have every checkbox with ng-model = "myModel", in the controller I set the value of the checkbox via $ scope.myModel = value, the value is set, but for all checkboxes at once, since they have one model. And how can they be identified individually? - Emm