Angularjs 如何通过输入字段将数值传递给函数?
我想制作一个产品列表,旁边有价格和数量字段,我希望用户能够在数量输入字段中输入一个数字,并查看调整后的价格,因此我提出了如下建议:Angularjs 如何通过输入字段将数值传递给函数?,angularjs,Angularjs,我想制作一个产品列表,旁边有价格和数量字段,我希望用户能够在数量输入字段中输入一个数字,并查看调整后的价格,因此我提出了如下建议: <li ng-repeat="product in products"> {{product.price}} - quantity: <input type="text" value="1" ng-model="prodQuantity"> - {{priceTotalProducts(prodQuantity)}}
<li ng-repeat="product in products">
{{product.price}} - quantity: <input type="text" value="1" ng-model="prodQuantity"> - {{priceTotalProducts(prodQuantity)}}
</li>
{{product.price}}-数量:-{{priceTotalProducts(prodQuantity)}
这肯定行不通。
请给我指出正确的方向 演示可能的解决方案
我稍微更改了标记,以便计算总计的函数将获取整个产品,并且将输入绑定到产品的数量
属性:
<body ng-controller="Ctrl">
<ul>
<li ng-repeat="product in products">
{{product.price}} - quantity: <input type="text" value="1" ng-model="product.quantity" />
- {{priceTotalProducts(product)}}
</li>
</ul>
</body>
您可以上传您在JSFIDLE上尝试过的内容吗?哪些不起作用?请尝试使用
function Ctrl($scope) {
$scope.products = [
{ name: "prod1", price: 1.55, quantity: 0 },
{ name: "prod2", price: 2.55, quantity: 0 },
{ name: "prod3", price: 3.55, quantity: 0 },
];
$scope.priceTotalProducts = function(product) {
return product.price * product.quantity;
}
}