Angularjs togglecheck error(not working as intended) with prechecked data

Posted by crozzfire on Stack Overflow See other posts from Stack Overflow or by crozzfire
Published on 2014-06-06T15:21:28Z Indexed on 2014/06/06 21:27 UTC
Read the original article Hit count: 243

I have this plunker where i have a button that opens a bootstrap modal dialog box.

I the modal, when a course is selected(checked) from this list, it adds 3 to the $scope.planned and also increases the progress bar accordingly. Similarly it also reduces in the same way when a checkbox is unchecked.

This is the function that does the above:

    $scope.toggleCheck = function (course) {
        //debugger
        var x = $scope.checkcoursefunction($scope.selectedCourses, course);
        if(x==true){
            $scope.selectedCourses.splice($scope.selectedCourses.indexOf(course), 1);
                $scope.planned -= 3;
        }
        else{
            if ($scope.selectedCourses.indexOf(course) === -1){
                $scope.selectedCourses.push(course);
                $scope.planned += 3;
            } else {
                $scope.selectedCourses.splice($scope.selectedCourses.indexOf(course), 1);
                $scope.planned -= 3;
            }
        }

            $scope.getPercentage();
    };

I have 2 services from where the controller fetches its data named Requirements and Planned Services. The table in the modal has a list of the requirements service data. I also have a function named checkplanneddetails() that checks if an item from this data is present in the requirements data. If present, they come in the table pre-checked. This is the function that checks:

        $scope.checkplanneddetails = function(course){
                $scope.coursedetail = course;
                $scope.requirementcoursename = ($scope.coursedetail.course.subject).concat("-",$scope.coursedetail.course.course_no);
                for(var k = 0; k < $scope.planneddetails.length; k++){
                    if($scope.requirementcoursename==$scope.planneddetails[k].course_name){
                      $scope.selectedCourses.push(course);
                        return true;
                    }
                }
                return false;
            };

        $scope.checkcoursefunction = function(arr,obj){
          return (arr.indexOf(obj) != -1);
        }

This works fine with bringing the data as checked. But the togglecheck() function does not work as they are supposed to with these checked details(they work in reverse).

It always returns true(for var x in togglecheck) even after the splice function. Am i splicing the course correctly?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about angularjs