Yoyosh Yoyosh - 1 month ago 12
AngularJS Question

Angular get in controller

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

Answer

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);
            });
    };
}