AngularJS - 脚本在承诺返回的工厂函数中停止运行 [英] AngularJS - Script stops running at factory function with promise return

查看:22
本文介绍了AngularJS - 脚本在承诺返回的工厂函数中停止运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习/从事一个新手项目,它基本上是一个简单的任务管理器(类似于那些待办事项列表项目).我为这个项目设计了一个用户登录页面,这是它的工作原理.

I've been learning/working on a newbie project which is basicly a simple task manager(similiar to those todo list projects). And I designed a user login page for this project and here is how it works.

所以我有两个函数 siteLogin() 用于登录和内部我想在用户登录后使用第二个函数 showTasks() 返回它获得的用户任务来自 API(我知道的承诺).

So I have two functions siteLogin() for logging in and inside it I want to use second function showTasks() after user logs in which returns the usertasks it gets from API (promises I know).

所以首先我需要从 showTasks() 函数内的 $http 返回一个值,但我最终返回了类似 $$state 所以我在这个网站上搜索并找到了几个解决方案,到目前为止我了解到 $http 不返回值但返回 promises.因此,经过几次尝试和失败后,我的代码现在运行到 showTasks() 并在那里停止.

So first I needed to return a value from a $http inside the showTasks() function but I ended up returning something like $$state so I searched and found couple of solutions on this website and so far I learned that $http doesn't return value but returns promises. So after couple of try&fail my code now runs until showTasks() and stops there.

现在这是我的代码.

工厂

app.factory('userTaskList', function ($http) {

    return {
        showTasks: function (userName) {
            var tasks = { K_ADI: userName }; //UserName of the per 
            var $promise = $http({
                method: 'POST',
                url: 'http://localhost:5169/api/Isler/' + userName + '/IstenilenKayitCek',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: tasks
            });
            $promise.then(function successCallback(response) {
                var data = response.data;
                console.log("Factory data:", response);
                return success(data);

            }, function errorCallback(response) {     
                error("Error");
            });
        }
    }
});

还有我的控制器:

 app.controller('myCtrl', ['$scope', '$http', function ($scope, $http,userTaskList ) {

 $scope.siteLogin = function () {
    var loginMember = {
        K_ADI: $scope.panel.loginUserName,  
        PAROLA: $scope.panel.loginPassword  // HTML input 
    };
    console.log(loginMember);
    $http({
        method: 'POST',
        url: 'http://localhost:5169/api/Kullanicilar/KullaniciDogrula',
        headers: {
            'Content-Type': 'application/json'
        },
        data: loginMember
    }).then(function successCallback(response) {
        console.log("Message sent", response);
        $scope.data = response.data.error.data;
        if ($scope.data === true) {

            console.log("User exists in database");

            //RUNS UNTIL HERE AND STOPS

            userTaskList.showTasks($scope.panel.loginUserName)
                .then(function (res) {
                    $scope.gorev = res;
                    console.log("Fonk ici : ", $scope.gorev);
                    console.log("222222", res);
                }, function (err) {
                    console.log(err);
                });
            console.log("outside func : ", $scope.gorev);     
        } 
    }, function errorCallback(response) {
        console.log("Error: ", response);
    });
}
}]);

这可能被视为重复,堆栈上有许多类似的问题,但我尝试了这些解决方案(稍后我会链接一些)仍然没有解决我的这个问题,而且其中一些还造成了其他类似的问题.我尝试使用 $q ,嵌套的 .then,最后在工厂中定义代码,然后在模块中调用它的实例等等.但是还是不行.

This may be seen like a duplicate and there are many similiar problems on stack but I tried those solutions (I will link some later) still didn't solve my this problem plus some of them created other problems like this one. I tried to use $q , nested .then, and finally defining code in factory then calling its instance in module and so on. But still not working.

注意:对不起,我的英语不好.

NOTE:Sorry for my poor English.

推荐答案

showTasks函数有几个错误:

app.factory('userTaskList', function ($http) {

    return {
        showTasks: function (userName) {
            var tasks = { K_ADI: userName }; //UserName of the per 
            var $promise = $http({
                method: 'POST',
                url: 'http://localhost:5169/api/Isler/' + userName + '/IstenilenKayitCek',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: tasks
            });
            var derivedPromise = $promise.then(function successCallback(response) {
                var data = response.data;
                console.log("Factory data:", response);
                ̶r̶e̶t̶u̶r̶n̶ ̶s̶u̶c̶c̶e̶s̶s̶(̶d̶a̶t̶a̶)̶;̶
                //IMPORTANT
                return data;

            }, function errorCallback(response) {     
                ̶e̶r̶r̶o̶r̶(̶"̶E̶r̶r̶o̶r̶"̶)̶;̶
                console.log(response.status);
                //IMPORTANT
                throw response;
            });
            //IMPORTANT
            return derivedPromise;
        }
    }
});

需要将所需数据返回给.then方法成功处理程序.

The desired data needs to be returned to the .then method success handler.

错误处理程序中的错误需要重新抛出.否则,被拒绝的承诺将转换为成功,值为 undefined.

Errors in the error handler need to be re-thrown. Otherwise the rejected promise will be converted to a success with a value of undefined.

最后需要将派生的 promise 返回给 showTasks 函数.

Finally the derived promise needs to be returned to the showTasks function.

您认为我在 $scope.siteLogin 中正确调用了该函数吗?

Do you think I call the function correctly inside the $scope.siteLogin ?

依赖注入错误:

̶a̶p̶p̶.̶c̶o̶n̶t̶r̶o̶l̶l̶e̶r̶(̶'̶m̶y̶C̶t̶r̶l̶'̶,̶ ̶[̶'̶$̶s̶c̶o̶p̶e̶'̶,̶ ̶'̶$̶h̶t̶t̶p̶'̶,̶ ̶
    function ($scope, $http,userTaskList ) {

应该

app.controller('myCtrl', ['$scope', '$http', 'userTaskList', 
    function ($scope, $http,userTaskList ) {

app.controller('myCtrl', function ($scope, $http,userTaskList ) {

这篇关于AngularJS - 脚本在承诺返回的工厂函数中停止运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆