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

【字号: 日期:2022-12-27浏览:19作者:雯心

问题描述

如何在外部获得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'); } }}]);

问题解答

回答1:

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

回答2:

// 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

相关文章: