将函数转换为 angular promise,因此 $scope.* 不是未定义的
Convert a function to a angular promise so $scope.* is not undefined
试图缩小我的代码,以便您清楚地看到我想要完成但没有成功的事情。
我想要完成的是,当我执行 console.log($scope.tempData);
时,我看到的是数据而不是未定义的数据,因此我可以在控制器中使用它。我已经知道承诺是要走的路,因为我在 Whosebug 上问了这个问题:$Scope variable is undefined
但我不知道如何从 $scope.getCube();
获得承诺,所以我可以做到 $scope.getCube().then(function (data) { ... });
我发现了这个:how to create asynchronous function 但答案不明确对我来说够多的了。
/////////// 附带问题 ////////////
我在底部提供的代码是我在 Github 上找到的代码,我并不是很清楚该代码的所有内容: 为什么 ClientService.dynamic() 一个承诺? this.dynamic = function (params)
中没有q.defer
?
////////////////////////////////////////// /
为了更好地阅读代码:JSFiddle
// CONTROLLER
$scope.buildObject = function () {
$scope.getCube(); //
console.log($scope.tempData); //gives undefined, obvious because getCube is not done running yet.
}
$scope.getCube = function () {
promise = ClientService.dynamic(params);
promise.then(function (data) {
$scope.tempData = data[0];
return $scope.tempData;
}
}
/////////////////////////////////////////////////////
// DYNAMIC SERVICE
this.dynamic = function (params) {
return qvCommService.send(createHyperCube).then(function (data) {
}
}
//////////////////////////////////////////////////
// SEND SERVICE
send: function (msg) {
var deferred = $q.defer();
var promise = deferred.promise;
this.socket = new WebSocket(ws://*************);
this.socket.addEventListener('open', function (e) {
deferred.resolve("connected")
});
this.socket.send(angular.toJson(msg))
return promise;
}
基本上你错过了 return 来自 getCube
函数的承诺 return,然后在调用 getCube
函数时你需要使用 .then
作用于它。这样您就可以在 .then
的成功回调中获得异步评估值
代码
$scope.getCube = function () {
promise = ClientService.dynamic(params);
promise.then(function (data) {
$scope.tempData = data[0];
return $scope.tempData;
}
return promise; //return promise object
};
$scope.buildObject = function () {
$scope.getCube().then(function(){
console.log($scope.tempData);
})
}
试图缩小我的代码,以便您清楚地看到我想要完成但没有成功的事情。
我想要完成的是,当我执行 console.log($scope.tempData);
时,我看到的是数据而不是未定义的数据,因此我可以在控制器中使用它。我已经知道承诺是要走的路,因为我在 Whosebug 上问了这个问题:$Scope variable is undefined
但我不知道如何从 $scope.getCube();
获得承诺,所以我可以做到 $scope.getCube().then(function (data) { ... });
我发现了这个:how to create asynchronous function 但答案不明确对我来说够多的了。
/////////// 附带问题 ////////////
我在底部提供的代码是我在 Github 上找到的代码,我并不是很清楚该代码的所有内容: 为什么 ClientService.dynamic() 一个承诺? this.dynamic = function (params)
中没有q.defer
?
////////////////////////////////////////// /
为了更好地阅读代码:JSFiddle
// CONTROLLER
$scope.buildObject = function () {
$scope.getCube(); //
console.log($scope.tempData); //gives undefined, obvious because getCube is not done running yet.
}
$scope.getCube = function () {
promise = ClientService.dynamic(params);
promise.then(function (data) {
$scope.tempData = data[0];
return $scope.tempData;
}
}
/////////////////////////////////////////////////////
// DYNAMIC SERVICE
this.dynamic = function (params) {
return qvCommService.send(createHyperCube).then(function (data) {
}
}
//////////////////////////////////////////////////
// SEND SERVICE
send: function (msg) {
var deferred = $q.defer();
var promise = deferred.promise;
this.socket = new WebSocket(ws://*************);
this.socket.addEventListener('open', function (e) {
deferred.resolve("connected")
});
this.socket.send(angular.toJson(msg))
return promise;
}
基本上你错过了 return 来自 getCube
函数的承诺 return,然后在调用 getCube
函数时你需要使用 .then
作用于它。这样您就可以在 .then
代码
$scope.getCube = function () {
promise = ClientService.dynamic(params);
promise.then(function (data) {
$scope.tempData = data[0];
return $scope.tempData;
}
return promise; //return promise object
};
$scope.buildObject = function () {
$scope.getCube().then(function(){
console.log($scope.tempData);
})
}