Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs$http.jsonp成功数据访问其他地方_Javascript_Angularjs_Angularjs Http - Fatal编程技术网

Javascript angularjs$http.jsonp成功数据访问其他地方

Javascript angularjs$http.jsonp成功数据访问其他地方,javascript,angularjs,angularjs-http,Javascript,Angularjs,Angularjs Http,在$http.jsonp()请求之外,如何访问$scope或从$http中成功获得的数据 $http.jsonp('http://example.com/?callback=JSON_CALLBACK') .success(function(data) { $scope.info1 = data.name; $scope.info2 = data.company; }); console.log("access it here outside:

$http.jsonp()
请求之外,如何访问$scope或从$http中成功获得的数据

$http.jsonp('http://example.com/?callback=JSON_CALLBACK')
    .success(function(data) {
        $scope.info1 = data.name;
        $scope.info2 = data.company;
    });

console.log("access it here outside: ",$scope.info1);
当前控制台打印未定义的内容


谢谢帮助。

不应考虑异步Ajax调用以同步方式工作。您必须等待ajax/承诺完成。虽然不要使用
.success
/
.error
它们已被弃用,但请使用
。然后使用
来连锁承诺

您必须依靠承诺来获得
解决
/
拒绝

代码

var promise = $http.jsonp('http://example.com/?callback=JSON_CALLBACK')
promise.then(function(response) {
    var data = response.data;
    $scope.info1 = data.name;
    $scope.info2 = data.company;
    console.log("access it here outside: ",$scope.info1);
    myOtherFunction($scope.info1);
})
.catch(function(error) {
    console.log(error);
});

我应该用什么来代替成功?请提供另一种方式好吗?thanksit仍然没有打印出来!我没有定义。你的控制台日志仍然在
然后
中,我的意思是完全在与
然后
$http
相关的代码之外,我想在同一控制器中的另一个函数中使用它。你必须从
调用另一个依赖函数。然后当前jsonp调用的
,你能举个例子吗?