【问题标题】:access a value outside a function访问函数外的值
【发布时间】:2015-08-06 21:21:36
【问题描述】:

我正在尝试从 url 获取数据到我的代码中。我在 Service.js 中进行这是我的代码

var demoService = angular.module('demoService', [])
.service('myService', function($http, $q) {

var v=0;

this.getdata = function() {
       $http.get('myurl').then(function(data) {
         v = data.data.Tweets[0].FAVOURITE_COUNT;

       });
      return v;  // how to return v from here(doesn't return the function's v)
     }
})

我想通过调用服务在我的 AngularJS 控制器中使用它:

  myService.getdata()//the v = data.data.Tweets[0].FAVOURITE_COUNT needed here

但问题是该函数返回 0(外部定义的 v)而不是我填充它的值!有没有一种方法可以只处理从 URL 返回的 Object 并传递一个值并返回它?

提前致谢!

【问题讨论】:

  • 您在 异步 $http.get() 调用处理之前返回了 v 值,这就是为什么您不断收到 0 作为结果。
  • @benderr : 请您分享一些关于如何解决此类错误的建议?
  • this.getData = function() { return $http.get('myurl'); } 然后myService.getData().then(function(data) { ... });
  • 您应该可以使用promises 解决问题,我现在无法构建示例,抱歉

标签: jquery angularjs http return


【解决方案1】:

$http.get 方法返回承诺。这意味着数据在您返回后被设置为“v”变量。因此,您应该仅在解决承诺后才能获取数据。例如:

angular.module('demoService', [])
     .service('myService', function($http, $q) {
     var data = null;
     this.fetchData = function() {
         $http.get('myurl').then(function(data) {
             data = data.data.Tweets[0].FAVOURITE_COUNT;
        });
     }
     this.fetchDataNoSave = function() {
         return $http.get('myurl');
     }
     this.getData = function() {
         return data;
     }
})

angular.module('demoService').controller('ViewCtrl', function(demoService, $scope) {
    demoService.fetchDataNoSave().then(function(data) {
         $scope.v = data.data.Tweets[0].FAVOURITE_COUNT;
    })
    // OR
    demoService.fetchData().then(function() {
         $scope.v = demoService.getData();
    })
})

这些选项的不同之处在于,第一个选项不会在服务中保存状态,并且您每次都应该获取此状态。第二个管理服务内的状态,这意味着每个注入服务的控制器都可以访问获取的数据

【讨论】:

    【解决方案2】:

    使用 Ajax 调用

    服务

    var demoService = angular.module('demoService', [])
    .service('myService',['$http', function($http) {
    
        this.getdata = function(entity){
            var promise = $http({
                method : 'POST',
                url : 'services/entity/add',
                data : entity,
                headers : {
                    'Content-Type' : 'application/json'
                },
                cache : false
            }).then(function (response) {
                return response;
            });
            return promise;     
        };
    }]);
    

    控制器:

    var demoService = angular.module('demoService', [])
    .controller('myctr',['$scope','myService',function($scope,myService){
       myService.getdata().then(function(response){
                //Success
    
            },function(response){
    
                //Error         
            });
    
    }]);
    

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      相关资源
      最近更新 更多