简体   繁体   English

在angularjs ng-repeat中调用函数

[英]Calling function inside angularjs ng-repeat

I am new to angularjs, have creating an phonegap project. 我是angularjs的新手,已经创建了phonegap项目。 In my project I am using WP JSON API User Plus Plugin to fetch data in json format, The API gives the response as few contents, also I need to call another API with parameter get from the previous API. 在我的项目中,我正在使用WP JSON API User Plus插件以json格式获取数据,该API提供的响应内容很少,而且我还需要调用另一个API,该API的参数与前一个API相同。 Is it possible to call an API inside an ng-repeat to load different datas. 是否可以在ng-repeat中调用API来加载不同的数据。

I am getting data's in json, and load the data in ng-repeat, I have tried like this 我在json中获取数据,并在ng-repeat中加载数据,我已经尝试过这样

<tr ng-repeat="app in multipleApps" ng-init="getActivationDate(app)">
    <td><h4> {{ app.Name }} </h4></td>
    <td> <img src="{{ app.ava_img }}"/> </td>
</tr>

which call the below function, 调用下面的函数,

$scope.getActivationDate = function(app) {
  Service.getActivationDate(app.name)
    .then(function(response) {
       var date = response.data.image;  
       $scope.app.ava_img = date;
     },
   // ...
};

the output what i am getting is display's the same image for all "app.ava_img", but the response from service is different. 我得到的输出是所有“ app.ava_img”显示相同的图像,但服务的响应不同。 please advise regarding the issue but I have no clue how to use it with ng-repeat. 请提供有关此问题的建议,但我不知道如何将其与ng-repeat一起使用。

It will be thankful, 会很感激的

Firstly, I encourage you to read the docs for ng-repeat . 首先,我建议您阅读ng-repeat的文档。 I assume app is an object and multipleApps is an array containing those objects. 我假设app是一个对象,而multipleApps是包含这些对象的数组。

$scope.app.ava_img = data; means you are setting the ava_img property on the app variable on your $scope to the data you have. 表示您正在$ scope上的app变量上将ava_img属性设置为拥有的数据。 So assign an array of objects to multipleApps. 因此,将对象数组分配给多个应用。

Eg: $scope.multipleApps = [ {name:'a', ava_img:'a.jpg' }, {name: 'b', ava_img: 'b.jpg' } ] 例如: $scope.multipleApps = [ {name:'a', ava_img:'a.jpg' }, {name: 'b', ava_img: 'b.jpg' } ]

Use 采用

<tr ng-repeat="app in multipleApps" ng-controller="ItemController">
    <td><h4> {{ app.Name }} </h4></td>
    <td> <img src="{{ app.ava_img }}"/> </td>
</tr>

and define a new controller in your app: 并在您的应用中定义一个新的控制器:

angular.module("yourapp").controller("ItemController", function($scope, Service){
    Service.getActivationDate($scope.app.name).then(function(response) {
         var date = response.data.image;  
         $scope.app.ava_img = date;
    }
});

Then, each iteration of ng-repeat will have its own ItemController instance with different $scope. 然后,每个ng-repeat迭代将具有不同的$ scope自己的ItemController实例。

I'm not entirely clear on what you're trying to accomplish but I get the gist of it. 对于您要完成的目标,我尚不完全清楚,但我的主旨是。

ng-init NG-INIT

Firstly, I would avoid using an ng-init to call a function. 首先,我将避免使用ng-init调用函数。 ng-init is usually something I'd use to initialize some trivial value on an object that's specific to helping the UI in some way. ng-init通常是我用来在某个对象上初始化某个琐碎值的东西,该值专门以某种方式帮助UI。 The docs even allude to this - https://docs.angularjs.org/api/ng/directive/ngInit 该文档甚至暗示这一点- https://docs.angularjs.org/api/ng/directive/ngInit

usual way to chain logic together - promises 将逻辑链接在一起的常用方法-Promise

As somebody who's new to Angular, you've probably encountered promises. 作为Angular的新手,您可能已经遇到了希望。 Anytime you're doing something like: 每当您在执行以下操作时:

someService.getSomething()
.then( function( rsp ){ ... })
.catch( function( err ){...})

... you're using the promises API. ...您正在使用promises API。 As I said earlier, I'm not entirely clear on your application so I will be making some assumptions as I go. 就像我之前说的,我对您的申请尚不完全清楚,所以我会做一些假设。

I assume multipleApps is the result of either an API call or you're fetching it from a local list? 我认为multipleApps是API调用的结果,还是您是从本地列表中获取的? Let's go with the API call. 让我们开始API调用。

appService.getApps()
.then( function( rsp ){
    return $scope.multipleApps = rsp.data.data //you structure may differ
})

So you're processing the rsp in your then handler. 因此,您要在then处理程序中处理rsp All we need to do is chain another series of promises together. 我们需要做的就是将另一系列的承诺链接在一起。 We could iterate through each app in the multipleApps , then assigning the proper value to the objects on the response, but angular provides us with a better way. 我们可以遍历multipleApps每个app ,然后为响应中的对象分配适当的值,但是angular为我们提供了更好的方法。 Let's use $q . 让我们使用$q $q provides us with the .all API. $q为我们提供了.all API。 Docs here - https://docs.angularjs.org/api/ng/service/$q#all 此处的文档-https: //docs.angularjs.org/api/ng/service/$q#all

Here's how I'd use it: 这是我的使用方式:

appService.getApps()
.then( function( rsp ){

    var multipleApps = rsp.data.data

    var rqsts = []
    for( var app, i = 0; i < multipleApps.length; i++ )
    {
        app = multipleApps[ i ]
        rqsts.push( appService.getActivationDate( app ))
    }

    $q.all( rqsts )
    //this triggers when ALL individual requests have returned
    .then( function( rsps ){

       //rsps is an array of rsp objects from the individual calls above
       for( var iRsp, i = 0; i < rsps.length; i++ )
       {
           iRsps = rsps[ i ]
           multipleApps[ i ].ava_img = iRsps.data.img
       }

       //we're assigning to scope after we've fetched all the data
       $scope.multipleApps = multipleApps
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM