1

I am using AngularJS v1.5.8, My requirement is when i click the Next button it'll display 'Processing...' inside button as text before complete the operation, i have included the $q with my services to get the asynchronous facility, but not working. please see my below codes.

Service

mainApp.factory('PINVerificationServices', ['$http', '$rootScope','$q', function ($http, $rootScope) {
    return {
        IsPermitted: function (param) {
            return $q($http({
                url: '/Api/ApiPINVerification/IsPermitted/' + param,
                method: 'POST',
                async: true
            }));
        }
    };
}]);

Controller

mainApp.controller('PINVerificationController', function ($scope, $rootScope, $state, $window,$q, PINVerificationServices) {
    $scope.SubmitText = "Next";

    $scope.Next = function () {
    $scope.SubmitText = "Processing...";
    PINVerificationServices.IsPermitted($scope.PIN).then(function (result) {
         $scope.SubmitText = "Next";
    });
  }
}

HTML

  <div class="list-group list-group-sm">
    <div class="list-group-item">
        <input class="form-control" ng-model="PIN" placeholder="PIN" required id="PIN" name="PIN" type="text" />
    </div>
  </div>
  <button type="submit" ng-click="Next()">{{SubmitText}}</button>
7
  • but not working - have you tried the simplest of debugging? like checking the developer tools console and network tabs? They provide a lot of information for real developers Commented May 24, 2017 at 11:50
  • I have checked @JaromandaX , its giving $q is not defined error. Thanks Commented May 24, 2017 at 11:52
  • i have included the $q with my services - right ... and did you include the q library in your page? Commented May 24, 2017 at 11:53
  • inject $q in factory, like : function ($http, $rootScope, $q) Commented May 24, 2017 at 11:53
  • 2
    $q isn't needed there at all, as $http already returns a promise.. Removing $q function wrapper should solve your problem Commented May 24, 2017 at 11:55

2 Answers 2

1

Try this:

 return $http({
                method: 'POST',
                url: '/Api/ApiPINVerification/IsPermitted/' + param
        });
Sign up to request clarification or add additional context in comments.

5 Comments

Not Working @Sergaros
can you show output of console.log(PINVerificationServices.IsPermitted($scope.PIN))
But another problem, not working with nested http service calling.
Suppose you have called IsPermitted($scope.PIN) function first, then again you have called another function PINVerificationServices.GetStudentInformationByPIN($scope.PIN) inside IsPermitted($scope.PIN) function.
Don't nest those calls. Just have the calls inside a function that passes a variable back and forth until you have verified the user.
0

Make below changes (from your requirement of nested $http).

In factory Use only $http, and no need of $rootScope as well, It should be like :

    mainApp.factory('PINVerificationServices', ['$http', function ($http) {
        return {
               IsPermitted: function (param) {
                return $http({
                    url: '/Api/ApiPINVerification/IsPermitted/' + param,
                    method: 'POST'
                   });
                },

               GetStudentInformationByPIN : function () {
                return $http({
                    url: '/Api/ApiPINVerification/GetStudentInformationByPIN /',//your api url
                    method: 'GET'
                    });
               }
          };
    }]);

In controller make use of $q.all() :

       mainApp.controller('PINVerificationController', function ($scope, $rootScope, $state, $window,$q, PINVerificationServices) {
       $scope.SubmitText = "Next";
       $scope.Next = function () {
            $scope.SubmitText = "Processing...";                      
            $q.all([PINVerificationServices.IsPermitted($scope.PIN),
            PINVerificationServices.GetStudentInformationByPIN($scope.PI‌N), 
             //other promises
            ]).then(function (result) {
                     if(result[0].data){
                         $scope.SubmitText = "Next";
                     }
                     if(result[1].data){
                         // studentdata response handling
                     }
                    });
            }
        }

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.