Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden Input and AngularJS ng-model binding

Tags:

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.

like image 228
Nick Mehrdad Babaki Avatar asked May 04 '15 05:05

Nick Mehrdad Babaki


People also ask

What is the difference between ng-model and data NG model?

The answer is: (ngModel) causes a 1-way data-binding, whereas [(ngModel)] ensures a two-way data binding.

What are hidden inputs?

<input type="hidden"> <input> elements of type hidden let web developers include data that cannot be seen or modified by users when a form is submitted. For example, the ID of the content that is currently being ordered or edited, or a unique security token.

What does [( ngModel )] do?

The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.

What is the difference between Ng bind and NG-model in AngularJS?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable. Save this answer.


1 Answers

In your case since you just want to use

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

Demo: Fiddle

like image 98
Arun P Johny Avatar answered Sep 28 '22 07:09

Arun P Johny