我正在尝试在 angularjs Controller 中创建一个乘法函数。我希望函数返回数量和价格的乘积。我正在使用下面的代码片段,但它返回了一个错误。我做错了什么?
<!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>
最佳答案
在这个简单的示例中,您不需要创建 Controller 。
参见 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>
但是,如果您需要创建它 - 查看错误:
- angulars 插值语法
{{ }}
仅用于 HTML,不用于 javascript。 - 使用
$scope
,而不是scope
- 格式化代码很重要。
关于javascript - 在 angularjs Controller 中创建乘法函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688991/