2

I am using Angular and $http request to get data from server. It is mock data in the same server directory but it return response in 20 sec. It is way to long. The request is executed on application start up. I have tried to execute the same query using jQuery $.ajax, and it worked in 10 ms. But I am want to get rid of jQuery. Why AngularJs $http is taking so long?

I am using Ifeanyi Isitor's Lazy Loading In AngularJS example how to use require.js with angular. loading my first view's controller points to service where query is executed. In chrome dev tools network traking I see it takes less when 10 ms to get file. but using console.time setting it before executing query and ending it at success promise it logs about 20000 ms. Can it be because of lazy loading? but why jQuery is working fast? here is code of my service

define(['appModule'], function(app) {
  app.lazy.factory('daoService', ['$http', function($http) {
    var ...
    ...
    getChanges = function(tableName, modifiedSince, callback) {
        console.log('data access time starts');
        console.time('data access time');
        // this works in 20000 ms
        $http.post(tablesUrl[tableName]).success(function(data) {
                console.log("The server returned " + data.length + " changes);
                console.timeEnd('data access time');
                callback(data);
            }).error(function(data){
                console.log(data);
            });

        /* this works in about 10 ms
        $.ajax({
            url: tablesUrl[tableName],
            dataType:"json",
            success:function (data) {
                console.log("The server returned " + data.length + " changes );
                console.timeEnd('data access time');
                callback(data);
            },
            error: function(model, response) {
                console.log(response);
            }
        });*/
    },
5
  • normally AngularJS is very fast. Please provide some code Commented Aug 28, 2013 at 14:02
  • It's impossible to know what is going on without seeing your code, but Angular is not the culprit. Commented Aug 28, 2013 at 14:02
  • Your server is the culprit. Not Angular. Commented Aug 28, 2013 at 14:20
  • It could also be something that locks in the front end. Please provide some code to help Commented Aug 28, 2013 at 14:33
  • Added some code end explanations Commented Aug 29, 2013 at 5:21

1 Answer 1

1

I have found answer. I was because some things in angular has to be invoked in order to be executes. For that purpose we need to use $apply. Here is the code:

define(['appModule'], function(app) {
    app.lazy.factory('daoService', ['$http', '$rootScope', function($http, $rootScope) {
    var ...
    ...
    getChanges = function(tableName, modifiedSince, callback) {
        $rootScope.$apply(function(){
                $http.post(tablesUrl[tableName], params).success(function(data) {
                    callback(data);
                }).error(function(data){
                    console.log(data);
                    phoneGapApp.showAlert(data.message);
                });   
            });
    },

More about $apply in Jim Hoskins's post

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

2 Comments

This should only be necessary if getChanges is called from outside of Angular (i.e. from a jQuery event or something similar).
Yes, it was implemented as service for accessing data and apparently it got outside of Angular. It was called from controller to get data from SQLite and sync data from server. Somewhere in the chain it got out of Angular.

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.