为什么我的If语句在AngularJS中的JS中不能100%工作

时间:2015-07-16 12:43:44

标签: javascript angularjs if-statement compare

enter image description here我有$scope.reasonList我用ng-repeat进行迭代。但是,如果我使用相同的数据返回和转发完全相同的页面,有时if操作符不起作用且$scope.reasonList为空。完全相同的数据和相同的页面?这些是我一直在比较的字符串。这里发生了什么,为什么?

var reasons = new Array();
var roots = new Array();
var repairs = new Array();
var defectDescsArray = new Array();
defectDescsArray= defectDescSvc.query();
$scope.rma = rmaService.get({id: $routeParams.rmaId});

var rmaHasDefects = rmaDefectSvc.findByRmaId({rmaid: $routeParams.rmaId});
    rmaHasDefects.$promise.then(function (result) {
        alert('result.length:' +result.length)

        for (var i = 0; i < result.length; i++) {

            for (var j = 0; j < defectDescsArray.length; j++) {

            if (result[i].rmaHasDefectdescPK.defectdescDefectdescId === defectDescsArray[j].defectdescId) {

                 $scope.showReasonList = true;
                 reasons.push(defectDescsArray[j]);

            }
         }

     }

       $scope.reasonList = reasons;
       //$scope.$apply();
       alert('$scope.reasonList.length' +$scope.reasonList.length);

                    });

查看

<div class="col-sm-10">
    <table class="table table-striped table-condensed table-hover">
        <tbody>
            <tr ng-repeat="defect in reasonList">

                <td>{{ defect.returnreasonReturnreasonId.returnText}}</td>
                <td>{{ defect.text}}</td>
            </TR>
        </TBODY>
    </table>
</div>

获取缺陷的角色服务

angular.module('defectDescService', ['ngResource'])
    .factory('defectDescSvc', ['$resource',
        function ($resource) {
            console.log('------defectDescService-----');
            return $resource(
                    '/RMAServerMav/webresources/com.rako.entity.defectdesc/:id',
                    {},
                    {
                         delete: { method: 'DELETE', params: {id: '@defectdescId'}}, 
                         update: { method: 'PUT', params: {id: '@defectdescId'} },
                         findByDefectdescId:{
                            url: '/RMAServerMav/webresources/com.rako.entity.defectdesc/defect/:defid',        
                            method: 'GET', 

                            params: {defid: '@defid'},
                            isArray:true}
                    });
        }]);

1 个答案:

答案 0 :(得分:1)

defectDescsArray 应用了承诺。 调用 rmaHasDefects promise函数,它也是一个promise。 rmaHasDefects 承诺保证在成功调用后执行正文代码。 但问题是,当执行代码运行时,您无法保证 defectDescsArray 是具有值的数组,因为promise调用未同步。这就是为什么,有时候一个承诺首先完成,这是正常情况,有时候另一个承诺首先完成,然后 reasonList 为空。 要证明这一点,请在承诺中记录 defectDescsArray ,并且您会看到它有时是空的,有时不是。

for (var j = 0; j < defectDescsArray.length; j++) {
    console.log('defectDescsArray length:',defectDescsArray.length);
    if (result[i].rmaHasDefectdescPK.defectdescDefectdescId === defectDescsArray[j].defectdescId) {

解决方案是通过在资源上使用promise来同步两个promise。

  defectDescsArray= defectDescSvc.query();
    //using a promise and second call is inside a promise to determine not empty //array. 
    defectDescsArray.$promise.then(function (result) {
        defectDescsArray = result;
    $scope.rma = rmaService.get({id: $routeParams.rmaId});

    var rmaHasDefects = rmaDefectSvc.findByRmaId({rmaid: $routeParams.rmaId});
    rmaHasDefects.$promise.then(function (result) {
        alert('result.length:' +result.length)

        for (var i = 0; i < result.length; i++) {

            for (var j = 0; j < defectDescsArray.length; j++) {

            if (result[i].rmaHasDefectdescPK.defectdescDefectdescId === defectDescsArray[j].defectdescId) {

                 $scope.showReasonList = true;
                 reasons.push(defectDescsArray[j]);

            }
         }

     }

       $scope.reasonList = reasons;
       //$scope.$apply();
       alert('$scope.reasonList.length' +$scope.reasonList.length);

                    });
});

我的帖子基于this