You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular控制器调用函数提示未定义错误求助

解决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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:51:58