【发布时间】: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