在angularjs视图中加上减去

时间:2016-04-09 06:00:51

标签: javascript angularjs

http://jsfiddle.net/4v2jaaxh/

git push staging selected_branch

如何在视图层中显示总数,因为我知道角度允许表达式如加或减。或者我应该在控制器级别执行此操作?

5 个答案:

答案 0 :(得分:2)

您可以在控制器中放置一个功能:

total:{{getSum()}}

然后在html中使用它:

- (void)saveSnapshot {
CGRect rect = self.tempimage.bounds;

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    if ([self.tempimage respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [self.imageview drawViewHierarchyInRect:rect afterScreenUpdates:YES]; // iOS7+
        [self.tempimage drawViewHierarchyInRect:rect afterScreenUpdates:YES];
    } else {
        [self.imageview.layer renderInContext:UIGraphicsGetCurrentContext()]; // pre iOS7
        [self.tempimage.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

以下是一个工作示例:jsFiddle

答案 1 :(得分:2)

您可以在控制器中输入一个功能(价格总计/数量总计 - 在本例中为示例价格总计):

angular.module('app',['QuickList']).controller('mainCtrl', function($scope){

  $scope.myJson = [
  {
    "id": "1",
    "name": "banana",
    "price": 12,
    "qty": 3,
  },
  {
    "id": "2",
    "name": "watermelon",
    "price": 12.9,
    "qty": 4,
  }]

  $scope.total = function(){
  var total = 0;
   for(var i = 0; i < $scope.myJson.length; i++){
    total += $scope.myJson[i].price;
   }
   return total;
  }

$scope.total();
})

以下是一个工作示例:http://jsfiddle.net/Lht7fodj/

答案 2 :(得分:0)

您可以使用加法作业

angular.forEach($scope.myJson, function(val) {
 $scope.totalPrice += val.price;
 $scope.totalQty += val.qty;
});

demo fiddle

答案 3 :(得分:0)

在控制器中,在定义数组后编写以下代码:

$scope.total = 0;
for(var i=0; i< $scope.myJson.length; i++){
    $scope.total += $scope.myJson[i].price * $scope.myJson[i].qty;
}

然后在您的视图中使用插值作为{{total}}

更好的方法是将上面的代码放在函数中并在插值语句中调用它。

答案 4 :(得分:0)

输入html:

</div><h1>
total:{{mytotal}}
</h1>

并在控制器中:

$scope.mytotal= $scope.myJson[0].price+$scope.myJson[1].price;