搜索

首页  >  问答  >  正文

angular.js - 如何获取angularjs中response的返回值?

如何在外部获得response的值?

app.controller('postCtrl', ['$scope', 'myservice', function($scope, myservice){
    myservice.getData().success(function(res){
        $scope.posts = res.records;
        console.log($scope.posts);  //这个有输出
    });
    console.log($scope.posts); //这个却显示undefined

}]);

app.factory('myservice', ['$http', function($http){
    return {
        getData: function(){
            return     $http.get("post_info.php");    
        }
    }
}]);
ringa_leeringa_lee2840 天前718

全部回复(2)我来回复

  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:11:16

    首先是作用域问题,要想外部访问可以试试$rootScope.posts=res.records;

    回复
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:11:16

    雷雷

    https://code.angularjs.org/1....$http

    回复
    0
  • 取消回复