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

AngularJS出现ReferenceError: findId未定义问题求助及相关疑问

Fixing ReferenceError: findId is not defined & Your AngularJS Service Questions

Let's break down your issues one by one, with practical fixes and clear explanations:

1. Resolving the findId is not defined Error

The core problem here is scope mismatch: in your _add function, you're trying to call findId() directly, but this function only exists in the object your factory returns—it isn't available in the immediate closure scope of the factory's internal code.

Here's how to fix this, plus an extra typo correction in your controller:

Updated Factory Code

app.factory('StorageService', function ($localStorage) {
    $localStorage = $localStorage.$default({
        things: [
            { id: 1, time: 'Oct 17, 2020 9:34:41 AM', message: '#hello world' },
            { id: 2, time: 'Oct 17, 2020 9:34:41 AM', message: 'COMPLETE THE IP PROJECT' },
            { id: 3, time: 'Oct 17, 2020 9:35:45 AM', message: '#hello world3' }
        ]
    });

    // Define helper functions first in the closure scope
    var _findId = function(id) {
        for (var item in $localStorage.things) {
            if ($localStorage.things[item].id === id) {
                return $localStorage.things[item];
            }
        }
    };

    var _newId = function() {
        let NewId; // Fix: Declare this variable to avoid global scope leakage
        if (NewId) {
            NewId++;
            return NewId;
        } else {
            var maxId = _.max($localStorage.things, function(thing) { return thing.id; });
            NewId = maxId.id + 1;
            return NewId;
        }
    };

    var _getAll = function () { return $localStorage.things; };
    var _add = function (thing) {
        var updatedItem = _findId(thing.id); // Use the internal _findId function
        if (updatedItem) {
            updatedItem.date = thing.date;
            updatedItem.message = thing.message;
        } else if(thing.message===""){ }
        else {
            thing.id = _newId(); // Use internal _newId function
            $localStorage.things.push(thing);
        }
    };

    var _remove = function (thing) {
        $localStorage.things.splice($localStorage.things.indexOf(thing), 1);
    };

    return {
        getAll: _getAll,
        add: _add,
        remove: _remove,
        findId: _findId, // Expose for external use
        newId: _newId
    };
});

Controller Typo Fix

You referenced noteService instead of your actual service name StorageService:

// Replace this line:
$scope.newContent = _.clone(noteService.findId(parseInt($routeParams.id)));
// With this:
$scope.newContent = _.clone(StorageService.findId(parseInt($routeParams.id)));

2. Factory vs Service: Do You Need to Switch?

Short answer: No, you don't need to switch to service.

Factory and service are just two different patterns for creating AngularJS services:

  • Factory: Returns a plain object/function that Angular injects. It's ideal for when you want full control over what gets exposed (which is exactly what you're doing here).
  • Service: Uses a constructor function—Angular instantiates it with new, so you attach methods to this.

Your current factory approach is perfectly valid; you just needed to adjust how you reference internal functions. Switching to service is a matter of personal preference, not a requirement to fix your error.

3. Why Couldn't You Submit This Question?

There are a few common reasons this might happen on Stack Overflow:

  • Duplicate detection: If your error message already has existing, high-quality answers, the site might prompt you to review those first before submitting.
  • Formatting issues: Code blocks might not have been properly wrapped, or your question title might not have been specific enough (Stack Overflow requires clear, descriptive titles).
  • Account restrictions: New accounts sometimes have limited posting privileges until you complete basic verification or gain small amounts of reputation.
  • Network issues: Temporary connectivity problems could prevent the form from submitting successfully.

内容的提问来源于stack exchange,提问作者Akash Vasu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 16:02:45