我正在尝试将按钮的名称从“暂停付款”更改为“未付款”,它应首先检查checkstatus.is_holdpayment的状态,然后执行函数userhold_payment()。我尝试了各种各样的东西,ng-show,ng-if但按钮的名称在点击时不会改变。点击它应该,Hold,Unhold,Hold,Unhold并执行相同的功能userhold_payment()。我想使用AngularJS更改按钮的名称。这是什么错误。这是main.html
<div ng-init="check_status()">
<button type="button" ng-if="checkstatus.is_hold_payment" ng-click="userhold_payment();" class="btn-success btn-lg hold_payment_html" style="margin-top:7px; font-size:16px">Hold</button>
<button type="button" ng-if="!checkstatus.is_hold_payment" ng-click="userhold_payment();" class="btn-success btn-lg hold_payment_html" style="margin-top:7px; font-size:16px">UnHold</button>
</div>
这是该功能的控制器。
$scope.userhold_payment = function() {
$http.get('http://localhost/ngaffiliate/api/payment/change_hold_payment_status')
.then(function(response) {
console.log('I called');
$scope.userhold = response.data;
console.log($scope.userhold.is_hold_payment);
});
};
//this will call when ever your controller reinstance
($scope.check_status = function() {
$http.get('http://localhost/ngaffiliate/api/payment/check_payment_status')
.then(function(response) {
console.log('I called now')
$scope.checkstatus = response.data;
// console.log(response);
console.log($scope.checkstatus.is_hold_payment);
if($scope.checkstatus.is_hold_payment=="0")
$scope.checkstatus.is_hold_payment = true;
else($scope.checkstatus.is_hold_payment=="1")
$scope.checkstatus.is_hold_payment = false;
$scope.checkstatus.is_hold_payment =!$scope.checkstatus.is_hold_payment;
console.log($scope.checkstatus.is_hold_payment);
});
})();
答案 0 :(得分:0)
尝试使用:
<button type="button" ng-click="userhold_payment();" class="btn-success btn-lg hold_payment_html" style="margin-top:7px; font-size:16px">{{checkstatus.is_hold_payment ?? "Hold": "Unhold"}}</button>
答案 1 :(得分:0)
正如我在评论中已经提到的,您应该在$scope.check_status()
函数中调用$scope.userhold_payment()
,因为该函数通常不会更改$scope.checkstatus.is_hold_payment
变量的值。我还删除了你反转变量值的行。
此处的Plunker演示:https://plnkr.co/edit/SWpdHGaNWIkyIWSvHyUU?p=preview