monitorService.getFMCMonitor({ scenario: 'indoor'},function(res){
$scope.data = res;
});
//$scope.$watch('data',(n,o)=>console.log(n));
console.log($scope.data); ==> undefined
monitorService
是一个获取后台数据的service
,如果不用$watch
,$timeout
,怎么同步$scope.data
的值?就是一打印$scope.data
,就是服务里取到的值,而不是undefined
Because it is asynchronous, the ajax request does not return when you print, so it is naturally undefined.
You can do this, pass a callback as a parameter, or a promise, both are fine.