REST API响应之前Angularjs服务函数返回响应

时间:2019-06-02 11:23:06

标签: javascript angularjs rest

Angularjs服务函数在Rest API响应之前返回响应。

app.factory('LoginService', function ($http, $state) {

    isAuthenticated = false;

    return {

        login: function (userName, password) {

            $http.get(gadgetConfigurations.server_url_local + apis.authenticate +
                    '/' + userName + '/' + password)

                .then(function (response) {

                        console.log('Response from Login api: ', response.data.length);

                        if (response.data.length > 0) {

                            isAuthenticated = true;

                            console.log('Response rec from rest api', isAuthenticated);

                        }

                    },

                    function (error) {

                        console.log('Login api error: ' + error);

                    })

            console.log('Response sent', isAuthenticated);

            return isAuthenticated;

        },



        isAuthenticated: function () {

            return isAuthenticated;

        }

    };

});

</code></pre>

// Response sent "false"

// Response rec from rest api "true"

0 个答案:

没有答案