Javascript 使用ng-bind的角度无限循环

Javascript 使用ng-bind的角度无限循环,javascript,angularjs,node.js,express,angularjs-ng-repeat,Javascript,Angularjs,Node.js,Express,Angularjs Ng Repeat,函数的作用是:返回一个具有summary属性的对象 我尝试了几种不同的方法来实现这一点,但我没有任何运气。为了解决这个问题,我要么将summary属性绑定到pack对象,要么将其显示在表中 在这段代码中,我首先从数据库中取出所有包并存储在$scope.packs中。然后,我想对每个包运行get_tracking()函数,并将summary属性应用于每个包 是否有一种方法可以在从数据库返回的每个包上运行函数?当我尝试在angular中运行函数时,我没有任何运气,因为包对象尚未返回 html: $s

函数的作用是:返回一个具有summary属性的对象

我尝试了几种不同的方法来实现这一点,但我没有任何运气。为了解决这个问题,我要么将summary属性绑定到pack对象,要么将其显示在表中

在这段代码中,我首先从数据库中取出所有包并存储在$scope.packs中。然后,我想对每个包运行get_tracking()函数,并将summary属性应用于每个包

是否有一种方法可以在从数据库返回的每个包上运行函数?当我尝试在angular中运行函数时,我没有任何运气,因为包对象尚未返回

html:


$scope.get\u跟踪函数是use$http.post,它是“promise”和“return response.summary”的类型;不向函数“get\u tracking”返回任何值,将summary属性绑定到其他位置,并对其使用双向绑定,以便在从
$http
调用返回结果时,可以显示它

<tbody>
    <tr data-ng-repeat="pack in packs"
         data-ng-show="((authentication.user) && (authentication.user._id == pack.user._id))">
       <td data-ng-bind="pack.tracking_number"></td>
       <td data-ng-bind="pack.description"></td>
       <td data-ng-bind="pack.company"></td>
       <td data-ng-bind="{{::get_tracking(pack)}}"></td>
       <td data-ng-bind="trackingSummary[packet]"></td>
       <td ng-bind=""></td>
    </tr>     
</tbody>
或:

这将防止无限摘要循环,因为调用对象的属性不会像函数那样每次都导致摘要。如果您的
包的顺序在加载后不会改变,您也可以传入索引并在对象本身上设置摘要:

var onLoadedFunc = function () {
    for (var i = 0; i < $scope.packs.length; i++) {
        $scope.get_tracking($scope.packs[i], i);
    }
};

$scope.get_tracking = function (packet, index) {
    if (packet){
        $http.post('/tracking', packet).success(function(response) {
            $scope.packs[i].summary = response.summary;
        }).error(function(response) {
        });
    } 
 };
var onLoadedFunc=函数(){
对于(变量i=0;i<$scope.packs.length;i++){
$scope.get_跟踪($scope.packs[i],i);
}
};
$scope.get_tracking=函数(数据包、索引){
如果(数据包){
$http.post('/tracking',packet).success(函数(响应){
$scope.packs[i].summary=response.summary;
}).错误(功能(响应){
});
} 
};
允许像这样的HTML:

[...]
<td data-ng-bind="pack.summary"></td>
[...]
[…]
[...]

您还可以从作用域中删除
get_tracking
函数,因为它只能由控制器中的代码调用,除非您需要在不重新加载作用域/视图的情况下更新或更改摘要。每次执行绑定时都会发送请求。在处理程序中,您更新触发绑定的数据,这就是为什么在应用程序中有无限循环


在传递到视图之前准备所有数据。获取每个包的跟踪信息,准备结构良好的对象,然后传递到视图。您将获得更好的性能。

我无法轻松访问面前的某些代码,因此这可能有一些问题需要解决,以防止多次调用服务器,但更多地将其视为一个概念。感谢您的帮助,这样做效果更好,而且我能够检索到我正在查找的值,但现在我得到了以下错误:错误:[$rootScope:infdig]10$digest()迭代已达到。流产!您能否添加一些导致
$digest
循环的更新代码我怀疑这是因为digest循环每秒检查一次值的变化,所以当它调用函数时,它每秒都调用一次,最后你会收到多个对服务器的请求排队,就我个人而言,我建议在加载控制器时为
$scope.packs
中的每个项目调用该函数,而不是在ng repeat中对其进行排列,以确保每页加载只调用一次。感谢您的帮助,我的编码很糟糕。我不应该像那样尝试在前端检索数据。谢谢你让我注意到这一点,我尝试了更多的使用它,但仍然无法让它工作。谢谢你的建议。我是个新手,读了很多书之后,我发现这是一个可怕的练习。我已经更新了我的代码并解决了这个问题。
$scope.trackingSummary = {};
$scope.get_tracking = function (packet) {
    if (packet){
        $http.post('/tracking', packet).success(function(response) {
            $scope.trackingSummary[packet] = response.summary;
        }).error(function(response) {
        });
    } 
 };
<tbody>
    <tr data-ng-repeat="pack in packs"
         data-ng-show="((authentication.user) && (authentication.user._id == pack.user._id))">
       <td data-ng-bind="pack.tracking_number"></td>
       <td data-ng-bind="pack.description"></td>
       <td data-ng-bind="pack.company"></td>
       <td data-ng-bind=""></td>
       <td data-ng-bind="get_tracking(pack)"></td>
       <td ng-bind=""></td>
    </tr>     
</tbody>

$scope.trackingSummary = {};
$scope.get_tracking = function (packet) {
    if (packet && !$scope.trackingSummary[packet.tracking_number]){
        $http.post('/tracking', packet).success(function(response) {
            $scope.trackingSummary[packet.tracking_number] = response.summary;
        }).error(function(response) {
        });
    } 
    return $scope.trackingSummary[packet.tracking_number];
 };
$scope.trackingSummary = {};

$scope.get_tracking = function (packet) {
    if (packet){
        $http.post('/tracking', packet).success(function(response) {
            $scope.trackingSummary[packet.tracking_number] = response.summary;
        }).error(function(response) {
        });
    } 
 };

 //after packs loaded
var onLoadedFunc = function () {
    for (var i = 0; i < $scope.packs.length; i++) {
        $scope.get_tracking($scope.packs[i]);
    }
};

//when you know that the packs collection is loaded:
 onLoadedFunc();
var onLoadedFunc = function () {
    for (var i = 0; i < $scope.packs.length; i++) {
        $scope.get_tracking($scope.packs[i], i);
    }
};

$scope.get_tracking = function (packet, index) {
    if (packet){
        $http.post('/tracking', packet).success(function(response) {
            $scope.packs[i].summary = response.summary;
        }).error(function(response) {
        });
    } 
 };
[...]
<td data-ng-bind="pack.summary"></td>
[...]