Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am fairly new to Angular and am working on incorporating an existing jQuery AJAX API into Angular.

Right now I am doing stuff like:

$.myApi.login('username', 'password').done(function(result) {
    //do stuff
    $scope.$apply(); //apply changes to Angular app
}).fail(function(result) {
    //do stuff if login failed
    $scope.$apply(); //apply changes to Angular app
});

Is there a cleaner way to integrate APIs like this than just writing $scope.$apply() everywhere? Can I integrate with Angular's $http somehow without ditching the existing functions and just making raw AJAX requests to the URLs (i.e. can I reuse the existing API's functions directly)?

share|improve this question
1  
Yes, just go ahead and use $http. Here is the documentation url docs.angularjs.org/api/ng/service/$http –  PSL 17 hours ago
    
@PSL So in this particular example, I would not even call $myApi.login() and instead just call the URL that $myApi.login() would have called, instead using $http.post('/loginUrl/') etc.? Is that pretty much the cleanest way to do it? Or can I reuse the jQuery functions somehow? –  Nick Tiberi 17 hours ago
    
Why do you want to use jquery ajax instead of angular. Then you are into the same business of scope.apply() right. You can do the ajax call with angular $http itself. If you really do want to use it then you would need to use deferred pattern instead of scop.apply to resolve/reject the promise. –  PSL 17 hours ago
    
I guess I just want to try to avoid rewriting all the API functions in Angular. I was hoping there would be some sort of plug and play way to do this, but based on your comments I guess I either have to make the raw requests using $http or just use $scope.$apply(). –  Nick Tiberi 17 hours ago
    
Yes you can either rewrite the service in angular or create a wrapper in angular and wrap it with angular $q deferred pattern or just the easiest $scope.$apply(). That is what i could think about, there might be another alternative but these are the only ones that comes to my mind. –  PSL 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.