简体   繁体   English

angular没有看到JSON数组中的所有对象

[英]angular doesn't see all objects inside JSON array

So I have this part of code 所以我有这部分代码

$scope.pauseChecked = function(monitorId, groupName) {
  for (var i = 0; i < $scope.monitorResults.length; i++) {
    if (document.getElementById('checkboxId').checked) {
      adminService.pauseMonitor(monitorId, groupName).then(function(response) {
        $scope.getMonitorResultsForSelectedGroups();
        $scope.filterResults();
        var keepGoing = true;
        angular.forEach($scope.monitorResults, function(monitor) {
          if (keepGoing) {
            if (monitor.id == monitorId && groupName == monitor.groupName) {
              monitor.triggerRunning = false;
              keepGoing = false;
            }
          }
        });
      });
    }
  }
};

inside of this row everything works fine 在此行内,一切正常

<tr ng-repeat="result in monitorResults" ng-click="setSelected(result.id)" ng-class="{highlighted: result.id === selectedRow, selected: checkboxId}">

this line is outside ng-repeat 这条线不在ng-repeat之外

<td ng-show="monitorResults.triggerRunning"><button class="btn btn-info" style="width: 150px" ng-click="pauseChecked(monitorResults.id, monitorResults.groupName)">Pause Checked </button></td>

and this how it is look on the page 以及页面上的外观

<table style="float:left; margin-left: auto; margin-right: auto;">
    <tr >
        <td><button class="btn btn-primary" ng-click="runAllMonitorsNew()" style="width: 150px">Run All Monitors</button></td>
        <td ng-show="!checkedGroup"><button class="btn btn-primary" style="width: 150px" ng-click="pauseGrMonitors(result.groupName)">Pause Monitors</button></td>
        <td ng-show="checkedGroup"><button class="btn btn-primary" style="width: 150px" ng-click="resumeGrMonitors(result.groupName)">Resume Monitors</button></td>
     </tr>
        <tr>
        <td><button ng-click="runChecked(result.id,result.groupName)" class="btn btn-info" style="width: 150px">Run Checked</button></td>
        <td ng-show="monitorResults[0].triggerRunning"><button class="btn btn-info" style="width: 150px" ng-click="pauseChecked(monitorResults.id,monitorResults.groupName)">Pause Checked </button></td>
        <td ng-show="!monitorResults[0].triggerRunning"><button class="btn btn-info" style="width: 150px" ng-click="resumeChecked(monitorResults.id,monitorResults.groupName)">Resume Checked</button></td>
    </tr>
</table>
<table style="float: right; margin-right: 50px">
    <tr>
        <td>
            <json-editor-input model="monitorResults" configuration="configuration" on-error="onError(err)"/>
        </td>
    </tr>
</table>
</div>
<BR>
        <img class="center-block" src="ajax-loader.gif" ng-show="loading"/>
<BR>
<table  class="table table-striped table-bordered">
   <tr>
        <td><B>Monitor Id</B></td>
        <td><B>Monitor Name</B></td>
        <td><B>Monitor Type</B></td>
        <td><B>Group Type</B></td>
        <td><B>Warn Threshold</B></td>
        <td><B>Error Threshold</B></td>
        <td><B>Monitor Result</B></td>
        <td><B>Compare Result</B></td>
        <td><B>Last Run Date</B></td>

    </tr>
       <tr ng-repeat="result in monitorResults" ng-click="setSelected(result.id)" ng-class="{highlighted: result.id === selectedRow, selected: checkboxId}">
        <td><input type="checkbox" ng-model="checkboxId" id="checkboxId" name="checkboxId"></td>
        <td>{{result.id}}</td>
        <td>{{result.name}}</td>
        <td>{{result.type}}</td>
        <td>{{result.groupName}}</td>
        <td>{{result.warnThreshold}}</td>
        <td>{{result.errorThreshold}}</td>
        <td>{{result.monitorResult}}</td>
        <td>  <p ng-style="changeColor(result.compareResult)">{{result.compareResult}}</p> </td>
        <td>{{result.lastRunTime}}</td>
        <td>  <button class="btn btn-primary" ng-click="runMonitorNew(result.id,result.groupName)">Run</button> </td>
        <td ng-show="result.triggerRunning"><button class="btn btn-primary"  ng-click="pause(result.id,result.groupName)">Pause</button> </td>
        <td ng-show="!result.triggerRunning"><button class="btn btn-primary" ng-click="resume(result.id,result.groupName)">Resume</button> </td>
    </tr>

this is I see in debugger 这是我在调试器中看到的

在此处输入图片说明

What you can see, I have replaced "result" on "monitorResults", because it is gave me to access an array, but thing is when I have checked code thru debugger I found out that monitorId and groupname is undefined. 可以看到,我已经在“ monitorResults”上替换了“结果”,因为它使我可以访问数组,但是事情是当我通过调试器检查代码时,我发现MonitorId和groupname是未定义的。 So then I have used monitorResults[0].id and monitorResults[0].groupName but it is gave me access just to the first element in array, how can get an access to each element? 因此,我使用了monitorResults [0] .id和monitorResults [0] .groupName,但是它只允许我访问数组中的第一个元素,如何获得对每个元素的访问权限?

Since you have two tables decoupled, you need to set a model in your scope when you check a result, and that model is the one you should reference in the pause buttons. 由于您有两个分离的表,因此在检查结果时需要在范围内设置一个模型,该模型是您应该在暂停按钮中引用的模型。 In fact, all of your monitor, run, and pause methods do not need to pass a variable in the methods, those methods should internally be referencing the scope variable checkboxId. 实际上,所有监视,运行和暂停方法都不需要在方法中传递变量,这些方法应在内部引用范围变量checkboxId。 It is the checking of the box that should set a result to true, for the other methods to use. 对于其他方法,应该选中复选框,将结果设置为true。

Make sense? 说得通?

EDIT 编辑

I would also change checkboxId to result.checked and set that true or false. 我还要将checkboxId更改为result.checked并将其设置为true或false。 Then the controller methods for the other actions should loop through the result list and look for a result that is checked, and use it. 然后,用于其他操作的控制器方法应遍历结果列表并查找已检查的结果并使用它。

Pattern 图案

The pattern to use for two decoupled tables like this is to have table 2 at the bottom set properties on the model in the array for the desired actions like "isChecked", "isPaused", or whatever. 用于两个这样的分离表的模式是在表中模型的底部设置表2,以执行所需的操作,例如“ isChecked”,“ isPaused”或其他。

Table 1 then has buttons that call controller methods. 然后,表1具有调用控制器方法的按钮。 Those methods loop through the array and check for the status of the toggled properties (isChecked, isPaused, or whatever). 这些方法在数组中循环并检查切换属性的状态(isChecked,isPaused或其他)。 Those methods then perform whatever the desired action is by calling another method and passing in the models they found meeting the criteria. 然后,这些方法通过调用另一个方法并传入他们发现符合条件的模型来执行所需的操作。

The view tables are agnostic to each other, and all the work of indentifying models happens in the controller. 视图表彼此不可知,并且识别模型的所有工作都在控制器中进行。 The only thing the view does is update properties on array items. 该视图唯一要做的就是更新数组项的属性。

Explaining your function: 解释您的功能:

$scope.pauseChecked = function(monitorId, groupName) { //you might be getting monitorId and group name properly
  for (var i = 0; i < $scope.monitorResults.length; i++) { //maybe you are looping to get all selected elements
    if (document.getElementById('checkboxId').checked) { //this will never work properly, because it will only find the first element(due to id which may/maynot be checked), plus why are you trying to do this?
      adminService.pauseMonitor(monitorId, groupName).then(function(response) { 
        $scope.getMonitorResultsForSelectedGroups();
        $scope.filterResults();
        var keepGoing = true;
        angular.forEach($scope.monitorResults, function(monitor) {
          if (keepGoing) {
            if (monitor.id == monitorId && groupName == monitor.groupName) {
              monitor.triggerRunning = false;
              keepGoing = false;
            }
          }
        });
      });
    }
  }
};

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

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