21

I want to use AngularJS to calculate total price for product when I add Quantity. I have a code as below:

<div ng-app="TheApp">
<div ng-controller="TheController">
    <input type="hidden" name="Price" value="100" ng-model="Price" />
    <input type="text" name="Quantity" ng-model="Quantity" />
    Total Price:{{TotalPrice}}
    <button class="btn btn-primary btn-block" ng-click="click()">Add Items</button>
</div>

Inside my controller I have:

    $scope.TotalPrice = $scope.Price * $scope.Quantity;

I know Angular does not support hidden but I am looking for the best practice to solve the issue. Please consider button is not for calculating TotalPrice but sending final result. I want it to be updated real time.

6 Answers 6

30

In your case since you just want to use

<input type="hidden" name="Price" ng-init="Price=100;" ng-value="Price"/>

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

There's no need for a hidden field. The init logic can be placed in the controller <div ng-controller="TheController" ng-init="Price=100;">
11

Angular just ignores hidden input elements.(ng-model doesn't support input field)

If you want that value in ng-model it self, then it shouldn't be of type hidden. You could solve this problem by hiding that div by doing display:none instead of giving type hidden or using ng-show directive make it hidden.

<div ng-controller="TheController">
    <input type="text" ng-show="false" name="Price" value="100" ng-model="Price" />
    <input type="text" name="Quantity" ng-model="Quantity" />
    Total Price:{{TotalPrice}}
    <button class="btn btn-primary btn-block" ng-click="click()">Add Items</button>
</div>

1 Comment

That's a good idea too, rendering a normal input field with ng-model but using a display:none. Clever work around suggestion.
2

I think there is no need to use a hidden value in the HTML, since it's not altered by user, you can simply initialize this value in controller's scope and update your "TotalPrice" value when the user adds any quantity or you can also use ng-init in html to initialize it, but it is not a good practice to use ng-init as stated by the docs.

https://docs.angularjs.org/api/ng/directive/ngInit

1 Comment

I was attempting to use a hidden input as well until I realized, you are correct, the data I needed was available by passing an entire object to the controller, like, Front End: ng-submit="newComment(post)" and Back-End: $scope.newComment = function(post) { console.log(post._id) }. This will pass the entire post object into the controller, and from there properties (like _id) can be accessed. Thanks for your answer!
0

if you want to do it without clicks

<input type="hidden" name="Price" value="100" ng-model="Price" ng-change="updateTotalPrice()" />
<input type="text" name="Quantity" ng-model="Quantity" ng-change="updateTotalPrice()" />

or if you want to do it on click

<button class="btn btn-primary btn-block" ng-click="addItem()">Add Items</button>

and then in addItem

function addItem(){
   //increment quantity 
   //call to updateTotalPrice()
}

Comments

0

You can pass Price value inside the click function as an argument like:

<div ng-app="TheApp">
<div ng-controller="TheController">

  <input type="text" name="Quantity" ng-model="Quantity" />
  Total Price:{{TotalPrice}}
  <button class="btn btn-primary btn-block" ng-click="click(100)">Add Items</button>
</div>

Or if you have your Product info loaded into $scope variable you just call it directly from controller and do not nedd to pass Price as hidden or in click function

$scope.Product  =  productFactory.get();

$scope.click = function() {
 $scope.TotalPrice = $scope.Product.Price * $scope.Quantity;
}

Comments

0
$scope.price = 10;

Use double brackets to capture the scope value.

<input type="hidden" name="Price" value="{{price}}"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.