Paypal按钮使用AngularJS一次提交多个项目
Paypal button submit multiple items at once with AngularJS
我有一个带有自定义cart
的angularJS应用程序,我正试图使用贝宝让用户一次签出他们的整个cart
。
HTML
<main ng-controller="CheckoutCtrl" class="prototype-paypal">
<h2 class="checkout-header">Checkout</h2>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="youremail@mail.com">
<input type="hidden" name="currency_code" value="CAD">
<div ng-repeat="item in custCart">
<input type="hidden" name="item_name_{{$index}}" value="{{item.name}}" >
<input ng-repeat="size in item.sizes track by $index" type="hidden" name="amount_{{$index}}"
value="{{size.price}}">
</div>
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit"
alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</main>
我认为我用错了$index
,这是我的数据:
$scope.custCart = [];
$scope.templateItems = [
{
name: 'First Item',
description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
src: 'http://placehold.it/150x150.gif',
type: 'Template',
sizes: [
{
size: "Small",
price: 3.99,
text: "size of item sm more details more",
numOrders: 0,
},
{
size: "Medium",
price: 5.99,
text: "size of item md more details more",
numOrders: 0,
},
{
size: "Large",
price: 7.99,
text: "size of item lg more details more",
numOrders: 0,
},
{
size: "X-Large",
price: 8.99,
text: "size of item xl",
numOrders: 0,
},
{
size: "XX-Large",
price: 10.99,
text: "size of item xxl",
numOrders: 0,
}
]
},
{
name: 'Second Item',
description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
src: 'http://placehold.it/150x150.gif',
type: 'Template',
sizes: [
{
size: "Small",
price: 3.99,
text: "size of item sm ",
numOrders: 0,
},
{
size: "Medium",
price: 5.99,
text: "size of item md more details more",
numOrders: 0,
},
{
size: "Large",
price: 7.99,
text: "size of item lg more details more",
numOrders: 0,
}
],
},
{
name: 'Third Item',
description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
src: 'http://placehold.it/150x150.gif',
type: 'Template',
sizes: [
{
size: "Small",
price: 3.99,
text: "size of item sm ",
numOrders: 0,
},
{
size: "Medium",
price: 5.99,
text: "size of item md more details more",
numOrders: 0,
},
{
size: "Large",
price: 7.99,
text: "size of item lg more details more",
numOrders: 0,
}
],
},
{
name: 'Fourth Item',
description: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
src: 'http://placehold.it/150x150.gif',
type: 'Template',
sizes: [
{
size: "Small",
price: 3.99,
text: "size of item sm ",
numOrders: 0,
},
{
size: "Medium",
price: 5.99,
text: "size of item md more details more",
numOrders: 0,
},
{
size: "Large",
price: 7.99,
text: "size of item lg more details more",
numOrders: 0,
}
],
}
];
问题是,由于某些原因,我的ng-repeat
循环没有显示custCart
中的第一个项目
问题不在ng repeat中。根据您的数据结构,它是正确的。我会改变你将数据添加到购物车的方式。目前,无论用户是否选择特定尺寸的商品,您都会将所有"商品"尺寸推到购物车中,而您只是使用ng类来隐藏数量为0的尺寸。这对我来说没有多大意义。
也就是说,您可以在视图中使用筛选器来筛选出数量为0的项目。您现在所做的只是使用CSS来隐藏这些值,而使用过滤器的区别在于,过滤器实际上不会为任何与过滤器不匹配的值生成任何DOM元素。
所以你可以在你的控制器中做这样的事情:
$scope.greaterThan = function(prop, val){
return function(item){
return item[prop] > val;
}
}
然后在你的重复中:
ng-repeat="size in item.sizes | filter: greaterThan('numOrders', 0) track by $index"
或者,您可以使用ngIf,它也不会呈现DOM元素,例如:
<input ng-repeat="size in item.sizes track by $index" ng-if="size.numOrders <= 0" type="hidden" name="amount_{{$index}}" value="{{size.price}}">
不过,我强烈建议您看看如何优化购物车功能。有一本非常好的书叫ProAngularJS。我似乎记得这本书使用的主要例子是通过创建一个网上商店一步一步来完成的。您可能只想为示例代码获取它。它肯定会帮助你简化你所拥有的,为购物车设置服务,并使用自定义指令,例如购物车下拉菜单。
您的HTML
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit"
alt="Make payments with PayPal - it's fast, free and secure!">
替换为:
<input type="submit" style="background:url(http://www.paypal.com/en_US/i/btn/x-click-but01.gif); width:100px; height:100px;" name="Submit"
alt="Make payments with PayPal - it's fast, free and secure!">
相关文章:
- 为什么我的ajax在提交用@HtmlRenderPartial加载的表单时只调用fire一次
- 通过一次提交保存来自多个 ASP.NET Telerik 网格的更改
- 双击时仅提交一次
- jQuery在表单仅提交一次后加载函数
- 一次提交两个 HTML 表单,一个在当前窗口中,一个在新窗口中
- 禁用/启用提交按钮,并在加载时每 1 秒运行一次此 JavaScript
- 一次提交即可提交多个表单
- 一次提交多个表单
- HTML/JQuery:按钮在根本不应该提交的时候提交一次
- ExtJS-3,Ext.grid.GridPanel,多行复选框:如何一次提交所有选中的项目
- 一次提交多个表单将中止除最后一个表单外的所有表单提交
- 即使用户一次又一次地输入同一封电子邮件,代码也不会停止提交表单
- 提交表单后仅刷新网页一次
- 我如何才能得到一个表单提交只有一次隐藏的值已经从谷歌地理编码服务更新
- AJAX在一次提交中发布来自克隆表单的数据
- 表单插件是提交所有的形式在一次
- 表单提交是提交一次,之后每次加倍
- 一次提交两份表格
- Paypal按钮使用AngularJS一次提交多个项目
- 如果上一次提交仍然有效,如何拒绝HTML表单提交