Angular - 将变量公开给控制器
Angular - Expose variable to Controller
所以我有一个控制器调用工厂然后设置 "variableToExpose"。当 I console.log($scope.variableToExpose) 出现时未定义。将该变量公开给控制器的最佳方式是什么?
.controller('SomeCtrl', function($scope, $http, $state, SomeFactory) {
console.log($scope.variableToExpose); //returns undefined
SomeFactory.getRequest($id).then(function(response){
$scope.variableToExpose = varFromGetRequest;
}
}
谢谢!
将 console.log
移到成功处理程序中。
app.controller('SomeCtrl', function($scope, $http, $state, SomeFactory) {
//console.log($scope.variableToExpose); //returns undefined
SomeFactory.getRequest($id).then(function successHandler(response){
//$scope.variableToExpose = varFromGetRequest;
$scope.variableToExpose = response;
console.log($scope.variableToExpose);
});
});
promise 的 .then
方法在 promise 已解决后调用成功处理程序函数。 $q
服务以承诺的解析值作为唯一参数调用该函数。
所以我有一个控制器调用工厂然后设置 "variableToExpose"。当 I console.log($scope.variableToExpose) 出现时未定义。将该变量公开给控制器的最佳方式是什么?
.controller('SomeCtrl', function($scope, $http, $state, SomeFactory) {
console.log($scope.variableToExpose); //returns undefined
SomeFactory.getRequest($id).then(function(response){
$scope.variableToExpose = varFromGetRequest;
}
}
谢谢!
将 console.log
移到成功处理程序中。
app.controller('SomeCtrl', function($scope, $http, $state, SomeFactory) {
//console.log($scope.variableToExpose); //returns undefined
SomeFactory.getRequest($id).then(function successHandler(response){
//$scope.variableToExpose = varFromGetRequest;
$scope.variableToExpose = response;
console.log($scope.variableToExpose);
});
});
promise 的 .then
方法在 promise 已解决后调用成功处理程序函数。 $q
服务以承诺的解析值作为唯一参数调用该函数。