[英]Angular.js: How do I use ng-bind to display concat. elements of an array as a string?
I am new to Angular and have a basic question about ng-bind that I couldn't find in the documentation. 我是Angular的新手,有一个关于ng-bind的基本问题,我在文档中找不到。 My scenario is based the shopping cart app in the O'Reily Angular.js book and I cannot seem to get ng-bind to work. 我的场景基于O'Reily Angular.js书中的购物车应用程序,我似乎无法让ng-bind工作。
Desired output: I need to modify my controller function so I can show my updated $scope.items array elements in a 'Grand Total' span. 期望的输出:我需要修改我的控制器功能,这样我就可以在'Grand Total'范围内显示更新的$ scope.items数组元素。
Here is the function: 这是功能:
function CartController($scope) {
$scope.items = [
{title: 'Software', quantity: 1, price: 1399.95},
{title: 'Data Package (1TB)', quantity: 1, price: 719.95},
{title: 'Consulting (per hr.)', quantity: 1, price: 75.00}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
},
$scope.reset = function(index) {
$scope.items = [
{title: 'Software', quantity: 0, price: 1399.95},
{title: 'Data Package (1TB)', quantity: 0, price: 719.95},
{title: 'Consulting (per hr.)', quantity: 0, price: 75.00}
];
};
}
I would recommend making a grandTotal
function on your $scope
and then binding that, like this: 我建议在$scope
上创建一个grandTotal
函数然后绑定它,如下所示:
http://jsfiddle.net/XMTQC/ http://jsfiddle.net/XMTQC/
HTML HTML
<div ng-app ng-controller="CartController">
Grand Total: <span>{{grandTotal()}}</span>
<br/>
Grand Total: <span ng-bind="grandTotal()"></span>
<br/>
</div>
JavaScript JavaScript的
$scope.grandTotal = function () {
return $scope.items.reduce(function (p, c) {
return p.price || p + c.price;
});
};
You can also use interpolation (instead of ngBind
) as indicated in the first span. 您也可以使用第一个范围中指示的插值(而不是ngBind
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.