lundi 20 juillet 2015

how to check if one checkbox is checked in angularjs to fire an xhr request

Hey gods of programming, I've a simple question...but for solving this i wen't around for more than 10 hours now... I even was up to page 20 on Google (an man....that was creepy!) for several search terms around this... i even have test all Answers here on stackoverflow.

How the hell can I check within an angular js controller, if a checkbox is checked or not?

Case: It's an App-GUI, the User have a Toggle-Styled-Checkbox (Cordova and IONIC Framework - it will be run on Android & iOs) and should be able to check or uncheck this toggle-checkbox. If the Checkbox is turning from true > false it should fire an xhr request to the api to safe that option. If checked from false > true the same.

My last two attemps:

<input type="checkbox" ng-model="foo.bar">


.controller('TimeTrackToolsOptionsCtrl', ['$scope', '$http', '$localstorage', '$ionicPopup', function ($scope, $http, $localstorage, $ionicPopup) {

    $scope.tracktimeactive = true; // initialize to unchecked

    $scope.$watch('foo.bar', function (value) {
        // value will contain either true or false depending on checked state
        console.log('checked', value);
    });


}]) // Ende TimeTrackToolsOptionsCtrl

and

<input type="checkbox" ng-model="_tmp" ng-checked="isChecked()" ng-change="stateChanged(_tmp)" />

.controller('TimeTrackToolsOptionsCtrl', ['$scope', '$http', '$localstorage', '$ionicPopup', function ($scope, $http, $localstorage, $ionicPopup) {

        $scope.tracktimeactive = true; // initialize to unchecked

        $scope.stateChanged = function (checked) {
            console.log("lalal");
            alert('checked: ' + checked);
        }

        $scope.isChecked = function() {
            console.log("true or false");
            // return true or false
        }


    }]) // Ende TimeTrackToolsOptionsCtrl

Serious... help me before i'm getting depressive.. please.

Greetings and thanks in advance, Tim




Aucun commentaire:

Enregistrer un commentaire