【问题标题】:Callback fires earlier than it should (Mean stack)回调比它应该更早触发(平均堆栈)
【发布时间】:2016-12-10 11:23:00
【问题描述】:

我有两个函数 getCharacterInfo(callback) 和 apply()。我在 getCharacterInfo 中调用 apply() 作为回调,但是 apply(回调)比它应该更早地触发(至少从我可以从代码中看出)

这是我的代码:

getCharacterInfo(回调)

$scope.getCharacterInfo = function(callback) {
    temp = $http.get('https://eu.api.battle.net/wow/character/' + $rootScope.current_user_realm + '/' + $rootScope.current_user + '?locale=en_GB&apikey=hidden');
    temp.then(function onSuccess(response){
        $scope.charInfo = response.data;
        $scope.charInfo.thumbnail = "https://render-api-eu.worldofwarcraft.com/static-render/eu/" + response.data.thumbnail
        console.log("$scope.charinfo = " + $scope.charInfo)
        console.log("response.data = " + response.data)
        if(callback) {
            callback();
        }
    })
}

申请()

$scope.apply = function() {
            $scope.newApplication.charName = $scope.charInfo.name;
            $scope.newApplication.realm = $scope.charInfo.realm;
            $scope.newApplication.armoryLink = 'http://eu.battle.net/wow/en/character/'+ $scope.charInfo.realm + '/'+ $scope.charInfo.name +'/advanced'
            console.log($scope.newApplication)
            applicationsService.save($scope.newApplication, function(){
                $scope.applications = applicationsService.query();
                $scope.newApplication = null;
            });
    };

函数调用来自 HTML 提交表单

<form ng-Submit="getCharacterInfo(apply())">

我得到的错误是在 apply() 中,控制台告诉我 $scope.charInfo 未定义。另外,characterInfo 中的 console.log() 永远不会被触发,我认为他们应该这样做吗?

我错了什么?

【问题讨论】:

    标签: javascript angularjs callback mean-stack


    【解决方案1】:

    传递不带括号的apply 参数:

    <form ng-submit="getCharacterInfo(apply)">
    

    否则,函数apply首先被执行,然后它的结果被传递给getCharacterInfo函数。

    【讨论】:

    • 成功了,谢谢好心的先生让我免于头痛!
    猜你喜欢
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多