ng-model在jsp页面中未更新值

huangapple go评论82阅读模式
英文:

ng-model not updating the value in jsp page

问题

I am trying to calculate the values : amountBeforeTax taxAmount and amount but my input named : amountBeforeTax is not updating value to $scope.amountBeforeTax in the controller.

Versions :

Angular JS : 1.2.20
jstl: 1.2
servlet-api: 2.5
java version : 1.8.0_261

What am I missing ?

My 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");
				}
			}]);

I have used ng-model and bi directional binding to show the value in realtime on the page but it is not updating.

My JSP : -

```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">
					<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>
							</form>
						</div>
					</div>
				</section>
			</div>
		</div>
	</div>
</div>
<script
	src="${pageContext.request.contextPath}/assets/js/CustomJs/whatsapp/client/userWhatsAppBuyCredits.js"></script>

I have tried debugging but could not find the issue in the code.

英文:

I am trying to calculate the values : amountBeforeTax taxAmount and amount but my input named : amountBeforeTax is not updating value to $scope.amountBeforeTax in the controller.

Versions :

Angular JS : 1.2.20
jstl: 1.2
servlet-api: 2.5
java version : 1.8.0_261

What am I missing ?

My JS : -

var app = angular.module(&#39;helloApp&#39;, []);
app
.controller(
&#39;HttpController&#39;,
[
&#39;$scope&#39;,
&#39;$rootScope&#39;,
&#39;$filter&#39;,
&#39;$location&#39;,
&#39;$window&#39;,
&#39;$http&#39;,
function($scope, $rootScope, $filter, $location,
$window, $http) {
$scope.apiKey = document.getElementById(&quot;apiKey&quot;).value;
$scope.amountBeforeTax = &#39;&#39;;
$scope.taxAmount = &#39;&#39;;
$scope.amount = &#39;&#39;;
$scope.showConfirmation = false;
$scope.makePayment = function() {
console.log(&quot;make payment start&quot;);
$scope.taxAmount = $scope.amountBeforeTax * 0.15;
console.log(&quot;amount before tax &quot; + $scope.amountBeforeTax);
$scope.amount = $scope.taxAmount + $scope.amountBeforeTax;
console.log(&quot;tax amount &quot; + $scope.taxAmount);
console.log(&quot;total amount &quot; + $scope.amount);
$scope.showConfirmation = true;
console.log(&quot;make payment end&quot;);
}
$scope.proceedPayment = function() {
console.log(&quot;proceedPayment payment start&quot;);
console.log(&quot;proceedPayment payment end&quot;);
}
}]);

I have used ng-model and bi directional binding to show the value in realtime on the page but it is not updating.

My JSP : -

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

I have tried debugging but could not find the issue in the code.

答案1

得分: 1

Sure, here's the translated content:

更改为:

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

视图

ng-model="$scope.amountBeforeTax.value"
英文:

change to:

$scope.amountBeforeTax = {value:&#39;&#39;};

view

ng-model=&quot;$scope.amountBeforeTax.value&quot;

huangapple
  • 本文由 发表于 2020年9月25日 15:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64059377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定