Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a multiplication function in an angularjs controller?

I am trying to create a multiplication function in an angularjs controller. I want the function to return the product of quantity and price. I'm using the snippet below, but it's returning an error. What am I doing wrong?

  <!DOCTYPE html> <html>

 <head> <script src= "Angular.js"></script> </head>

 <body>

 <div ng-app="" ng-controller="personController">

 <h2>Cost Calculator</h2>

 Quantity: <input type="number" ng-model="quantity"> Price: <input type="number" ng-model="price">

 <p><b>Total in dollar:</b> {{fullname()}}</p>

 </div>


 <script> function personController(scope) {    var input1 =  scope.quantity ,    var input2 = scope.price ,
     $scope.fullName = function() {
         return {{input1 * input2}};
     } } </script>

 </body> </html>
like image 429
Toxiedo212 Avatar asked May 14 '26 22:05

Toxiedo212


1 Answers

In this simple example you do not need to create a controller.

See here

<h2>Cost Calculator</h2>
Quantity:
    <input type="number" ng-model="quantity">Price:
    <input type="number" ng-model="price">
    <p><b>Total in dollar:</b> {{quantity*price}}</p>
</div>

But, if you need create it - see mistakes:

  • angulars interpolation syntax {{ }} is only used in HTML, not javascript.
  • Use $scope, not scope
  • formatting your code is important.
like image 133
Ivan Zhukov Avatar answered May 17 '26 11:05

Ivan Zhukov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!