Angular控制器调用函数提示未定义错误求助
Hey there! Let's get that "函数未定义" error sorted out for you. The root cause here is pretty straightforward: when you define a function on $scope in an Angular controller, you can't call it directly by name inside the controller—you need to reference it through the $scope object.
问题分析
You defined $scope.findprojectBarcode as your lookup function, but when you tried to call it in $scope.postRequest, you used findprojectBar... (without the $scope. prefix). Since the function isn't in the controller's local scope (it's attached to the $scope object), JavaScript can't find it, hence the error.
修正后的代码示例
Here's how to adjust your postRequest function to call the lookup correctly:
$scope.findprojectBarcode = function (p) { var prjBarCode = ""; var i = 0; for (i = 0; i < $rootScope.projectList.value.length; i++) { if ($rootScope.projectList.value[i].Name === p) { console.log($rootScope.projectList.value[i].Name); return $rootScope.projectList.value[i].Barcode; } } return prjBarCode; }; $scope.postRequest = function () { // 替换成你要传入的实际项目名称参数 var targetProject = "Your Target Project Name"; // 关键:通过$scope调用自定义函数 var prjCode = $scope.findprojectBarcode(targetProject); // 后续的POST请求逻辑... };
额外提示
If you were calling this function from your Angular view (like in an ng-click directive), you could use just findprojectBarcode() directly—Angular automatically looks up functions on the $scope for view bindings. But inside the controller itself, you have to explicitly reference $scope to access those functions.
内容的提问来源于stack exchange,提问作者trx

