[英]Lodash in Angular JS - getting TypeError: Object function lodash(value)
i've created an application in angular for performing a simple calculator. 我已经创建了一个角度应用程序,用于执行简单的计算器。 The application works fine ( demo ) but here in the controller within the calculate method i've wrote some arithmetic calculation in javascript.
该应用程序工作正常( 演示 ),但在计算方法的控制器中我已经在javascript中写了一些算术计算。 So to make my angular code clean i created another file named common.js where i placed that arithmetic calculations.
因此,为了使我的角度代码清洁,我创建了另一个名为common.js的文件,其中我放置了算术计算。 I'm using lodash.js.
我正在使用lodash.js。 but when i tried to call the method
_.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
但是当我试图调用方法
_.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
i'm getting the following exception. 我得到以下例外。
TypeError: Object function lodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? ...<omitted>... } has no method 'calculateResult'
at Object.$scope.calculate (http://localhost:8080/RestSample/app/scripts/controllers.js:18:19)
can anyone please tell me some solution for this. 任何人都可以告诉我一些解决方案。
common.js common.js
(function() {
var calculateResult = function(no1, no2, opp) {
var A = parseInt(no1);
var B = parseInt(no2);
var C = 0;
switch (opp) {
case '+':
C = A + B;
break;
case '-':
C = A - B;
break;
case '*':
C = A * B;
break;
case '/':
C = A / B;
break;
}
return C;
}
})();
controllers.js controllers.js
var app = angular.module('app', []);
app.controller("appController", function($scope){
$scope.operators = ['+', '-', '*', '/'];
$scope.selectedOperator = $scope.operators[0];
$scope.calculate = function() {
$scope.result = _.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
};
});
index.html 的index.html
<div ng-app="app">
<div ng-controller="appController">
<div class="offset4 span6 well">
<label>Enter a value :</label>
<input ng-model="firstNumber" type="text"> <br/><br>
<label>Another Value:</label>
<input ng-model="secondNumber" type="text"> <br/><br>
<label>Operator:</label>
<select ng-model="selectedOperator"
ng-options="operator for operator in operators"></select>
<br><br>
<button ng-click="calculate()">Calculate</button> <br><br>
Result: {{result}}
</div>
</div>
</div>
Try ngLodash module, initiate it like you would with any Angular module / plugin. 尝试ngLodash模块,像使用任何Angular模块/插件一样启动它。 It works complelty without touching window scope and stays with the normal Angular way of loading.
它可以在不触及窗口范围的情况下完成工作,并保持正常的Angular加载方式。
https://github.com/rockabox/ng-lodash https://github.com/rockabox/ng-lodash
It's on bower also 它也在凉亭上
Bower install nglodash Bower安装nglodash
Add it into to the html file with a script tag and load it in to the module. 使用脚本标记将其添加到html文件中并将其加载到模块中。
Why should it work? 它为什么要起作用?
When you use lodash, the _
symbol is usually bound to lodash so you are calling a method on the lodash library with this code: 使用lodash时,
_
符号通常绑定到lodash,因此您使用以下代码在lodash库上调用方法:
_.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
And indeed the error message confirms that _
is lodash. 确实错误消息确认
_
是lodash。
Moreover, your common.js
file never exports calculateResult
to some place accessible to code which is outside your anonymous function. 此外,您的
common.js
文件永远不会将calculateResult
导出到匿名函数之外的代码可访问的某些位置。 You could export it on window
if you add this statement at the end of your anonymous function window.calculateResult = calculateResult
and then you'd call it with: 如果在匿名函数
window.calculateResult = calculateResult
的末尾添加此语句,则可以在window
导出它,然后用以下方法调用它:
window.calculateResult($scope.firstNumber, $scope.secondNumber, $scope.selectedOperator );
This should take care of your immediate problem. 这应该照顾你的直接问题。 (
window
is actually optional in window.calculateResult(...)
because without it the JavaScript interpreter will still seek calculateResult
from the global scope but I prefer to make it explicit.) (
window
在window.calculateResult(...)
实际上是可选的,因为没有它,JavaScript解释器仍然会从全局范围中寻找calculateResult
,但我更喜欢将它显式化。)
I would strongly suggest looking into a proper modularization system to avoid polluting the global scope with functions like calculateResult
. 我强烈建议寻找一个合适的模块化系统,以避免使用
calculateResult
函数污染全局范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.