将 PayPal 结帐按钮的样式更改为 Angular.js 组件?
Posted
技术标签:
【中文标题】将 PayPal 结帐按钮的样式更改为 Angular.js 组件?【英文标题】:Changing the style of a PayPal Checkout Button as an Angular.js component? 【发布时间】:2018-08-01 07:57:38 【问题描述】:当PayPal Checkout Button 用作Angular.js element 指令时,如何设置style
选项?
this.paypal =
// ...
style:
color: 'black',
shape: 'rect'
似乎style
选项不能作为style
在绑定中传递,因为这已经是保留的htmlElement
属性?
<paypal-button
client="$ctrl.paypal.client"
commit="true"
env="$ctrl.paypal.env"
style="$ctrl.paypal.style"
on-authorize="$ctrl.paypal.onAuthorize"
on-cancel="$ctrl.paypal.onCancel"
payment="$ctrl.paypal.payment">
</paypal-button>
【问题讨论】:
【参考方案1】:知道了,你必须使用ng-attr-style="$ctrl.paypal.style"
,它会起作用。
ng-attr-style
允许您计算表达式,而不是解释该输入元素的style
属性的字符串文字。完整的解释可以在标题“用于绑定到任意属性的 ngAttr”下的 here 中找到。
【讨论】:
【参考方案2】:请参阅plnkr link 了解在指令内的paypalbutton上应用样式的工作代码
您可以将整个 paypal 变量传递给控制器范围内的指令。
scope:
paypal: '='
,
然后你可以将变量绑定到模板
<test-directive paypal="$ctrl.paypal"></test-directive>
(function()
var app = angular.module("myApp", ['ui.bootstrap', 'paypal-button']);
app.controller('testCtrl', ['$scope', function($scope)
var $ctrl = this;
$ctrl.paypal =
env: 'sandbox',
client:
sandbox: 'AWi18rxt26-hrueMoPZ0tpGEOJnNT4QkiMQst9pYgaQNAfS1FLFxkxQuiaqRBj1vV5PmgHX_jA_c1ncL',
production: '<insert production client id>'
,
payment: function()
var env = this.props.env;
var client = this.props.client;
return paypal.rest.payment.create(env, client,
transactions: [
amount:
total: '1.00',
currency: 'USD'
]
);
,
commit: true, // Optional: show a 'Pay Now' button in the checkout flow
onAuthorize: function(data, actions)
// Optional: display a confirmation page here
return actions.payment.execute().then(function()
// Show a success page to the buyer
);
,
style:
color: 'black',
shape: 'rect'
;
]);
app.directive('testDirective', [function ()
return
restrict: 'E',
template: '<paypal-button env="paypal.env" style="paypal.style" client="paypal.client" payment="paypal.payment" commit="paypal.commit" on-authorize="paypal.onAuthorize"></paypal-button>',
scope:
paypal: '='
,
link: function (scope, element, attrs, controller)
;
]);
());
<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@3.0.0" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
<link data-require="bootstrap@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script data-require="angular.js@1.6.6" data-semver="1.6.6" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.min.js"></script>
<script src="https://www.paypalobjects.com/api/checkout.js" data-version-4></script>
</head>
<body ng-app="myApp">
<div ng-controller="testCtrl as $ctrl">
<test-directive paypal="$ctrl.paypal">
</test-directive>
</div>
</body>
</html>
【讨论】:
感谢您的回答,但我并不想覆盖 PayPal Checkout 按钮的样式(这很困难,因为它通过 iframe). I was simply looking for how to set the
style` 参数嵌入到页面中Angular.js 组件,给定 style
已经是一个有效的 HTMLElement 属性。以上是关于将 PayPal 结帐按钮的样式更改为 Angular.js 组件?的主要内容,如果未能解决你的问题,请参考以下文章