0

I am learning Angular. The following code works:

.controller('abc', function ($scope, $http)
{
$http.get("/Handlers/Authentication.ashx")
  .success(function (data)
  {
    alert(data);
  })

This function however does not:

.controller('abc', function ($scope, $http)
{
$scope.run = function ($scope, $http)
{
    $http.get('/Handlers/Authentication.ashx');
    //  .success(function (data)
    //{
    //  alert(data);
    //});
};
}

I know that I should use a service here. But for learning purposes I would like to know why it does not work to call this function inside:

<body ng-app="MainModule">
<div ng-controller="abc">
<div>
    <button type="button" class="btn btn-info" ng-click="run();">{{xx}}</button>

Thank you for help in advance

2 Answers 2

1

You're overriding controller injected $http service here :

$scope.run = function ($scope, $http)
{
    $http.get('/Handlers/Authentication.ashx');
    //  .success(function (data)
    //{
    //  alert(data);
    //});
};

Just remove all arguments on your scope function and it should work :

.controller('abc', function ($scope, $http) {
    $scope.run = function () {
        $http.get('/Handlers/Authentication.ashx')
            .success(function (data){
                alert(data);
            });
    };
}
Sign up to request clarification or add additional context in comments.

Comments

0

Because you are getting data, but don't doing with it something.

$http.get('/Handlers/Authentication.ashx').then(function(data){
       console.log(data);
}, function(err){
   console.log(err);
 })

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.