简体   繁体   English

将对象从函数返回到AngularJS中的ng-repeat

[英]return object from Function to ng-repeat in AngularJS

I am trying to return an object from a function to ng-repeat: 我试图将一个对象从函数返回到ng-repeat:

<ul ng-controller="Users" ng-repeat="user in users">
    <li ng-controller="Albums" ng-repeat="album in getAlbumList(user.name)">
        {{album.title}}
    </li>
</ul>

.controller('Albums', ['$scope','Imgur', function($scope, Imgur) {
    $scope.getAlbumList = function(user) {
        Imgur.albumList.get({user:user},    
            function(value) {
                return value.data;
                console.log('success');
            },
            function(error) {
                console.log('something went wrong');
            }
        );
    }
}]);

.factory('Imgur', function($resource, $http) {
    return {
        albumList : $resource('https://api.imgur.com/3/account/:user/albums'),
        album : $resource('https://api.imgur.com/3/account/:user/album/:id')
    }
});

This approach however crashes my browser on every page load. 然而,这种方法会在每次加载页面时崩溃我的浏览器。

What is the right way to do this? 这样做的正确方法是什么?

So, you have ng-repeat="album in getAlbumList(user.name)" , which is an Angular expression. 所以,你有ng-repeat="album in getAlbumList(user.name)" ,这是一个Angular表达式。 What ng-repeat does is evaluates getAlbumList(user.name) on every digest. ng-repeat的作用是在每个摘要上计算getAlbumList(user.name) This is just like any other expression in a view, such as ng-show or whatever, and those expressions get evaluated all the time . 这就像视图中的任何其他表达式一样,例如ng-show或其他任何表达式,并且这些表达式始终被评估。 If your expression is slow to evaluate hundreds of times, you shouldn't have it in an expression like this. 如果你的表达式评估数百次很慢,你不应该在这样的表达式中使用它。

What you have here is an Ajax call in that function. 你在这里有一个函数中的Ajax调用。 Even if you somehow had a blocking synchronous Ajax call, it would be orders of magnitude too slow. 即使你以某种方式进行了阻塞同步Ajax调用,它的数量级也会太慢。 Expressions should just access data already at hand, or maybe run simple code to sort and filter. 表达式应该只是访问手头的数据,或者运行简单的代码来排序和过滤。 They should never try to make Ajax calls. 他们永远不应该尝试进行Ajax调用。

But that's assuming it's synchronous Ajax, which this isn't. 但这是假设它是同步Ajax,而事实并非如此。 The expression simply calls getAlbumList and assumes the result is an array and uses it for ng-repeat. 该表达式只调用getAlbumList并假设结果是一个数组并将其用于ng-repeat。 That function returns nothing, so it returns undefined , and you get nothing. 该函数什么都不返回,所以它返回undefined ,你什么也得不到。

So to answer your question, what you should do is, put an empty array on scope, use that from the ng-repeat, and fill it in via Ajax. 所以要回答你的问题,你应该做的是,在范围上放一个空数组,使用ng-repeat中的数组,然后通过Ajax填充它。 The beauty of Angular means the data will just "pop in" in the view once it's there. Angular的美妙之处在于,一旦它存在,数据就会在视图中“弹出”。 Here's some view code: 这是一些视图代码:

<ul>
  <li ng-repeat="album in albums">{{album.title}}</li>
</ul>

And some controller code: 还有一些控制器代码:

$scope.albums = [];
Imgur.albumList.get({user:user},
    function(value) {
        $scope.albums = value.data;
        console.log('success');
    },
    function(error) {
        console.log('something went wrong');
    }
);

Which is basically the code you already had, just not run from a function, but run right in the constructor of the controller. 这基本上就是你已经拥有的代码,只是不是从函数运行,而是在控制器的构造函数中运行。

Edit 编辑

I think your fundamental misunderstanding is thinking that returning something from an ngResource callback will in turn return it from the containing function (getAlbumList). 我认为你的根本误解是认为从ngResource回调中返回一些东西会反过来从包含函数(getAlbumList)返回它。 That's not the case. 事实并非如此。 The containing function runs, returns nothing, then later on your callback runs, returns something, but no one is using that return value. 包含函数运行,不返回任何内容,然后在回调运行时,返回一些内容,但没有人使用该返回值。

<ul ng-controller="Users">
     <li ng-repeat="user in users">
       <ul ng-controller="Albums" ng-init="getAlbumList(user.name)">
          <li ng-repeat="album in albums">
           {{album.title}}
          </li>
       </ul>
     <li>
</ul>


.controller('Albums', ['$scope','Imgur', function($scope, Imgur) {
    $scope.getAlbumList = function(user) {
        Imgur.albumList.get({user:user},    
          function(value) {
                $scope.albums = value.data;
                console.log('success');
          },
          function(error) {
              console.log('something went wrong');
          }
      );
    }}]);

Try this. 试试这个。 If not then check this question Reusable components in AngularJS 如果没有,那么请检查此问题AngularJS中的可重用组件

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

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