【问题标题】:ng-model not updating the value in jsp pageng-model 不更新 jsp 页面中的值
【发布时间】:2024-04-20 03:15:02
【问题描述】:

我正在尝试计算值:amountBeforeTax taxAmount 和amount,但我的输入名为:amountBeforeTax 没有在控制器中将值更新为$scope.amountBeforeTax。

版本:

Angular JS:1.2.20 jstl:1.2 小服务程序 API:2.5 java版本:1.8.0_261

我错过了什么?

我的 JS:-

var app = angular.module('helloApp', []);

app
    .controller(
        'HttpController',
        [
            '$scope',
            '$rootScope',
            '$filter',
            '$location',
            '$window',
            '$http',
            function($scope, $rootScope, $filter, $location,
                $window, $http) {

                $scope.apiKey = document.getElementById("apiKey").value;
                
                $scope.amountBeforeTax = '';
                $scope.taxAmount = '';
                $scope.amount = '';
                
                $scope.showConfirmation = false;

                $scope.makePayment = function() {
                    console.log("make payment start");
                    $scope.taxAmount = $scope.amountBeforeTax * 0.15;
                    console.log("amount before tax " + $scope.amountBeforeTax);
                    $scope.amount = $scope.taxAmount + $scope.amountBeforeTax;
                    console.log("tax amount " + $scope.taxAmount);
                    console.log("total amount " + $scope.amount);
                    $scope.showConfirmation = true;
                    console.log("make payment end");
                }
                
                $scope.proceedPayment = function() {
                    console.log("proceedPayment payment start");
                    console.log("proceedPayment payment end");
                }
            }]);

我已使用 ng-model 和双向绑定在页面上实时显示该值,但它没有更新。

我的 JSP:-

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!-- userWhatsAppBuyCredits.jsp -->
<input type="hidden" value="${pageContext.request.contextPath}"
    id="contextPath">
<input type="hidden" id="apiKey" value="${apiKeyAttribute}">
<!-- start:main -->
<div class="container">
    <div id="main">
        <!-- start:breadcrumb -->
        <ol class="breadcrumb">
            <li><a
                href="${pageContext.request.contextPath}/reseller/clientres.jsp">Home</a></li>
            <li class="active">Buy Credits</li>
        </ol>
        <div class="row" id="home-content">
            <div class="col-lg-12" style="margin: 25px;">Buy Credits</div>
            <div class="col-lg-12" style="margin: 25px;" ng-if="!showConfirmation">
                <label for="paymentAmount"></label>
                <input name="amountBeforeTax"
                    id="amountBeforeTax" ng-model="amountBeforeTax"> <br>
                <button ng-click="makePayment()">Purchase Credits</button>
            </div>
            <div class="col-lg-12" style="margin: 25px;">
                bT : {{ amountBeforeTax  }} tax : {{ taxAmount }} amount {{ amount }}
            </div>
            <div class="col-lg-12" style="margin: 25px;" ng-if="showConfirmation">
                <section class="panel">
                    <!-- <header class="panel-heading">Credits Information </header> -->
                    <div class="panel-body" id="tablediv">
                        <div class="adv-table">
                            <table width="50%">
                                <tr>
                                    <td
                                        style="color: white; font-weight: 600; background: #2b7eb4;"><b>Amount</b></td>
                                    <td
                                        style="color: white; font-weight: 600; background: #2b7eb4;"><b>R{{ amountBeforeTax}}</b></td>
                                </tr>
                                <tr>
                                    <td>VAT @ 15%</td>
                                    <td>R{{ taxAmount }}</td>
                                </tr>
                                <tr>
                                    <td
                                        style="color: white; font-weight: 600; background: #2b7eb4;"><b>Total
                                            Amount to be paid in ZAR</b></td>
                                    <td
                                        style="color: white; font-weight: 600; background: #2b7eb4;"><b>R{{ amount }}</b></td>
                                </tr>
                            </table>
                            <hr>
                            <label>Amount to be paid:R {{ amount}} ZAR</label>
                            <hr>
                            <form action="MakePayment">
                                <button type="submit" class="btn btn-success" ng-click="proceedPayment()">
                                    Proceed<i class="fa fa-arrow-circle-right"></i>
                                </button>
                                <!-- </div> -->
                            </form>
                            <!-- end:breadcrumb -->
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>
<script
    src="${pageContext.request.contextPath}/assets/js/CustomJs/whatsapp/client/userWhatsAppBuyCredits.js"></script>

我尝试过调试,但在代码中找不到问题。

【问题讨论】:

    标签: java angularjs jsp-tags angularjs-ng-model java-ee-8


    【解决方案1】:

    改为:

    $scope.amountBeforeTax = {value:''};
    

    查看

    ng-model="$scope.amountBeforeTax.value"
    

    【讨论】:

    • 我试图用上面的答案更改代码,它不起作用,你能帮我更好地理解你的答案,为什么我不能直接使用 ng-model="amountBeforeTax" ?跨度>
    • 我尝试使用 ng-model="amountBeforeTax.value" 而不是 ng-model="$scope.amountBeforeTax.value" 并且成功了。感谢您的帮助