AngularJs ng-repeat ng-init ng-show条件中断一个表行

时间:2015-07-24 14:19:22

标签: json angularjs

在ng-repeat中如何检测一个JSON值是否发生变化?

如果区域更改,我将在表格中显示一行,其中包含前一列值的总和。

Ng-init接收区域值,但始终与控制器上创建的当前区域的值相同,因此无法进行比较。

m = 2*2;  %// Here m is the product of the dimensions of each matrix you want to shuffle
[~,I] = sort(rand(m,n));  %// This is just a trick to get the equivalent of a vectorized form of randperm as unfortunately randperm only accepts scalars
idx = reshape(I,2,2,n);
D_shuffled = D(idx);

我有一个带有区域,月份值和年度值的JSON,格式如下:

ng-init="previousRegion = uniqueitem.Region"

ng-show="{{ uniqueitem.Region !== previousRegion }}"

我的重复:

[
    {
        "Region": "NORTH",
        "VlM": "654321",    
        "VlY": "123456"
    },
    {
        "Region": "NORTH",
        "VlM": "234567",    
        "VlY": "432765"
    },
    {
        "Region": "SOUTH",
        "VlM": "567234",    
        "VlY": "321456"
    },
    {
        "Region": "CENTER",
        "VlM": "456123",    
        "VlY": "987654"
    }
]

而不是简单的列表

<table>
    <tbody ng-repeat="item in data track by item.Region">
        <tr ng-repeat="uniqueitem in item" >
            <td ng-init="previousRegion = uniqueitem.Region">
              {{uniqueitem.Region}} - {{previousregion}} - 
            </td>
            <td>
              {{uniqueitem.VlM}} 
            </td>
            <td>
              {{uniqueitem.VlY}} 
            </td>
             <td 
                ng-show="{{ uniqueitem.Region !== previousRegion }}"
                ng-init="previousRegion = uniqueitem.Region">
                <b>Total</b> here will came totals
             </td>
        </tr>
    </tbody>
</table>

我将建立一个列表,每个区域打破一行,对总和进行求和。

在控制器中,我确实创建了范围变量:

Region  M           Y 
NORTH   654321  123456
NORTH   234567  432765
SOUTH   567234  321456
CENTER  456123  987654

想要找到破解条件来开始总和。

$scope.previousRegion ='';
$scope.sumM ='';
$scope.sumY ='';
$scope.sumMRegion ='';
$scope.sumYRegion ='';
$scope.sumMRegionTotals ='';
$scope.sumYRegionTotals ='';

2 个答案:

答案 0 :(得分:1)

试试这个

ng-init中的

ng-init="newRegion = previousRegion!=uniqueitem.Region ;previousRegion= uniqueitem.Region"

in total field

 ng-show="newRegion"

答案 1 :(得分:1)

ng-repeat为每次迭代使用$ index值,您可以在比较中使用它:

{{ uniqueitem.Region !== item[$index - 1].Region }}