0

How to call web service with AngularJS on checkbox change event. here is my code

angular.module('myApp', []).controller('checkBoxCtrl', ['$scope', function ($scope) { 

    $scope.SelectedMobileCateg = function () {
        alert($scope.SelectedMobID);
        debugger;
        angular.module('myApp', []).controller("ProductController", function ($scope, $http) {
            $http.get('http://localhost:50622/api/GetMobile/getMobiles').
              success(function (data) {
                  $scope.products = data;
                  debugger;
              }).
              error(function (data) {
                  debugger;
                  alert("erro");
              });
        });
    };

}]);

I will be very thanks full to you

3
  • 1
    Wow wow wow, why do you have a controller only for a checkbox in the first place ? Commented Jul 24, 2017 at 8:26
  • What are you trying to achieve ? It's not maybe the good solution Commented Jul 24, 2017 at 8:30
  • @Alburkerk covered it pretty well in his answer, so I just have a side note: Using success and error on the $http service is deprecated since they don't return a promise and thus doesn't chain. Use then instead. Commented Jul 24, 2017 at 8:39

1 Answer 1

2

I think you need to learn more about Angular structure first..

Take a look at John Papa guidelines (easy to find), to learn what should be a controller or not.

Controller are made to be associated with a view / state, ie a big component. So it is very unlikely to be associated with only a checkbox (because it's only a boolean after all).

It you only need to call an http request from multiple endpoints, take a look at what services or factory are for. Just define your http request in a factory, and call it from multiple controllers.

If you really need to spend messages between different controllers, which is unlikely to be the case with your description of your problem, you can use $scope documentation and search about $scope.emit, $scope.broadcast etc..

Here's an example of their use. But as I said, learn more first.

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

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.