【问题标题】:How to get two json value from one ngrepeat?如何从一个 ngrepeat 中获取两个 json 值?
【发布时间】:2017-09-02 13:37:47
【问题描述】:

我想在一次 ngrepeat 中获取两个 JSON 数组值。

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
   $scope.res ={};
   $scope.res.fsus = [
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "var"
        },
       "time": {
          "timeA": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "car"
        },
       "time": {
          "timeA": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "car"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "car"
        },
        "time": {
          "timeA": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "ban"
        },
       "time": {
          "timeA": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  }
];

$scope.opts = [];
angular.forEach($scope.res.fsus,function(key,value){
  if(($scope.opts.indexOf(key.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode , 0)) <= -1){    $scope.opts.push(key.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode);
  };
if(($scope.opts.indexOf(key.statusMessageType.MasterConsignment.time.timeA , 0)) <= -1){    $scope.opts.push(key.statusMessageType.MasterConsignment.time.timeA);
  };
});
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<li ng-repeat="test in opts">
  <span class="step">    {{test}}
  </span> 
  <span class="title">    {{???}}
  </span>
</li>
</body>

我也想改变这次的格式YYYY-DD-MM@HH:MM。

感谢您的帮助。

【问题讨论】:

  • 你的回答有帮助吗?

标签: javascript angularjs json angularjs-ng-repeat


【解决方案1】:

您可以使用 angular.forEach 创建具有状态和时间的对象并在 ng-repeat 中使用它

$scope.opts = [];
angular.forEach($scope.res.fsus,function(key,value){
if((($scope.opts.indexOf(key.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode , 0)) <= -1) && (key.statusMessageType.MasterConsignment.time)){    $scope.opts.push({'status':key.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode,'time':key.statusMessageType.MasterConsignment.time.time});
  };
});

演示

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
   $scope.res ={};
   $scope.res.fsus = [
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "var"
        },
       "time": {
          "time": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "car"
        },
       "time": {
          "time": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "car"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "car"
        },
        "time": {
          "time": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  },
  {
    "statusMessageType": {
      "MasterConsignment": {
        "ReportedStatus": {
          "ReasonCode": "ban"
        },
       "time": {
          "time": "2017-10-01T10:15:00.000Z"
        }
      }
    }
  }
];

$scope.opts = [];
angular.forEach($scope.res.fsus,function(key,value){
if((($scope.opts.indexOf(key.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode , 0)) <= -1) && (key.statusMessageType.MasterConsignment.time)){    $scope.opts.push({'status':key.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode,'time':key.statusMessageType.MasterConsignment.time.time});
  };
});
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<li ng-repeat="test in opts">
  <span class="step">    {{test.status}}
  </span> 
  <span class="title">    {{test.time  | date:'MM/dd/yyyy @ h:mma'}}
  </span>
</li>
</body>

【讨论】:

  • 它正在工作,但是。它在给我。重复值也。但是之前的代码删除了重复的值。
  • 您只需要添加支票
  • @Sajeestharan。感谢您的评论。但每件事都和以前一样。应该可以了。
【解决方案2】:

这可能对你有帮助:

var jsonArray='your json array here';
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js"></script>
    <body ng-app="testApp" ng-controller="testCtrl">
    <li ng-repeat="test in jsonArray">
      <span class="step">    {{test.statusMessageType.MasterConsignment.ReportedStatus.ReasonCode}}
      </span> 
      <span class="title">    {{test.statusMessageType.MasterConsignment.time.timeA  | date:'MM/dd/yyyy @ h:mma'}}
      </span>
    </li>
    </body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2022-08-24
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多