angular.js - Bagaimana untuk mendapatkan nilai pulangan respons dalam angularjs?
ringa_lee
ringa_lee 2017-05-15 17:09:16
0
2
696

Bagaimana untuk mendapatkan nilai tindak balas secara luaran?

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_lee
ringa_lee

ringa_lee

membalas semua(2)
淡淡烟草味

Yang pertama ialah isu skop Jika anda mahukan akses luaran, anda boleh mencuba $rootScope.posts=res.records;

phpcn_u1582
// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

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

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan