来自 AngularJS 的 $success 回调函数 [英] $success call back function from AngularJS

查看:45
本文介绍了来自 AngularJS 的 $success 回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用控制器以获取 API 值.如何在 $http 方法之外传递数组?

我需要将一个数组 pa[] 传递给 $scope.myData = pa;.

首先,console.log(pa) 打印值 [10,20,30,40].

其次,console.log(pa)清空数组[].

JavaScript

function Ctrl($scope, $http) {var url = "https://spreadsheets.google.com/feeds/list/0AsXjbsnPIu06dGswZUV4WX/values?alt=json-in-script&callback=angular.callbacks._0";var pa = [];$http({方法:'JSONP',网址:网址}).成功(功能(数据){for (i = 0; i < data.feed.entry.length; i++) {var entry = data.feed.entry[i];pa.push(entry.gsx$productivity.$t);控制台日志(pa);//[10,20,30,40,50]}});console.log(pa)//[] 空数组$scope.myData = pa;}

如何在 $success 回调函数之外获取数组?

解决方案

此代码是异步的.在 $http 服务有机会从您的 API 调用中获取值之前,pa 已分配给 $scope.myData.

您需要使用 $q 服务用于控制代码流的承诺库.像这样:

function Ctrl($scope, $http, $q) {var url = "https://spreadsheets.google.com/feeds/list/0AsXjbsnPIu06dGswZUV4WX/values?alt=json-in-script&callback=angular.callbacks._0";var pa = [];var paPromise = $q.defer()$http({方法:'JSONP',网址:网址}).成功(功能(数据){for (i = 0; i < data.feed.entry.length; i++) {var entry = data.feed.entry[i];pa.push(entry.gsx$productivity.$t);控制台日志(pa);//[10,20,30,40,50]}paPromise.resolve(pa)});$scope.myData = paPromise.promise;}

在这里,我们注入 $q 服务并使用它实例化一个变量 paPromise.接下来,我们将此承诺提供给 $scope.myData.一旦承诺在 $http 成功方法中得到解决,AngularJS 将通知您的 $scope 并更新值,它将反映在您的模板/DOM 上.>

I am calling the controller to get the API value. How do I pass the array outside of the $http method?

I need to pass an array, pa[], to the $scope.myData = pa;.

First, console.log(pa) prints the value [10,20,30,40].

Second, console.log(pa) empties array[].

JavaScript

function Ctrl($scope, $http) {
    var url = "https://spreadsheets.google.com/feeds/list/0AsXjbsnPIu06dGswZUV4WX/values?alt=json-in-script&callback=angular.callbacks._0";
    var pa = [];
    $http({
        method: 'JSONP',
        url: url
    }).success(function (data) {

        for (i = 0; i < data.feed.entry.length; i++) {
            var entry = data.feed.entry[i];
            pa.push(entry.gsx$productivity.$t);
            console.log(pa); //[10,20,30,40,50]
        }
    });

    console.log(pa) // [] empty array

    $scope.myData = pa;
}

How do I get the array outside of the $success callback function?

解决方案

This code is asynchronous. pa is assigned to $scope.myData before the $http service has had a chance to get the value from your API call.

You need to use the $q service promise library to control the flow of your code. Something like this:

function Ctrl($scope, $http, $q) {
  var url = "https://spreadsheets.google.com/feeds/list/0AsXjbsnPIu06dGswZUV4WX/values?alt=json-in-script&callback=angular.callbacks._0";
  var pa = [];
  var paPromise = $q.defer()

  $http({
    method: 'JSONP',
    url: url
  }).success(function(data) {

      for (i = 0; i < data.feed.entry.length; i++) {
        var entry = data.feed.entry[i];
        pa.push(entry.gsx$productivity.$t);
        console.log(pa); //[10,20,30,40,50]
      }
      paPromise.resolve(pa)
    });

  $scope.myData = paPromise.promise;
}

Here, we inject the $q service and instantiate a variable paPromise using it. Next, we give this promise to $scope.myData. Once the promise gets resolved inside the $http success method, AngularJS will notify your $scope and update the value and it'll be reflected on your template/DOM.

这篇关于来自 AngularJS 的 $success 回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆