首页 > 解决方案 > Angularjs ReferenceError:未定义函数

问题描述

我得到ReferenceError: findId is not defined这个代码

var app = angular.module('sample', ['ngRoute', 'ngStorage']);
app.config(function($routeProvider) {
    $routeProvider
        .when('/noteTemp', {
            templateUrl: 'noteTemp.html',
            controller: 'MainCtrl'
        })
        .when('/newNote', {
            templateUrl: 'newNote.html',
            controller: 'MainCtrl'
        })
        .when('/newNote/edit/:id', {
            templateUrl: 'newNote.html',
            controller: 'MainCtrl'
        })
        .otherwise({
            redirectTo: '/noteTemp'
        });
});
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' }]
    });
    var _getAll = function () {
        return $localStorage.things;
    };
    var _add = function (thing) {
        var updatedItem = findId(thing.id);
        if (updatedItem) {
            updatedItem.date = thing.date;
            updatedItem.message = thing.message;
        } else if(thing.message===""){
            
        } else {
            thing.id = newId();
            $localStorage.things.push(thing);
            
        }
        
    }
    var _remove = function (thing) {
        $localStorage.things.splice($localStorage.things.indexOf(thing), 1);
    }
    
    return {
        getAll: _getAll,
        add: _add,
        remove: _remove,
        findId: function(id) {
            for (var item in $localStorage.things) {
                if ($localStorage.things[item].id === id) {
    
                    return $localStorage.things[item];
                }
            }
        },
        newId:  function() {
            if (NewId) {
                NewId++;
                return NewId;
            } else {
                var maxId = _.max($localStorage.things, function(thing) {
                    return thing.id;
                });
                NewId = maxId.id + 1;
                return NewId;
            }
        }
    };
});
app.controller('MainCtrl', function ($scope, $routeParams, StorageService, $location) {
    $scope.things = StorageService.getAll();
    
    if (!$routeParams.id) {
        $scope.newContent = { id: 0, time: new Date(), message: '' };
    } else {
        $scope.newContent = _.clone(noteService.findId(parseInt($routeParams.id)));
    }

    $scope.add = function () {
        StorageService.add($scope.newContent);
        $location.path('/noteTemp');
    };
    $scope.remove = function (thing) {
        StorageService.remove(thing);
        $location.path('/noteTemp');
    };
});

我认为错误是我无法调用工厂内的函数,我还想问我是否应该使用服务而不是工厂.....这是我所拥有的所有细节,请接受这个问题。.为什么我不能发布这个问题是我的重要疑问......请接受......

标签: javascriptangularjs

解决方案


如果调用上下文是可靠的,你可以参考this获取返回的对象,并从中获取findId返回对象的属性:

var _add = function (thing) {
    var updatedItem = this.findId(thing.id);

如果调用上下文不可靠,请findId预先定义为独立函数:

function findId(id) {
    for (var item in $localStorage.things) {
        if ($localStorage.things[item].id === id) {

            return $localStorage.things[item];
        }
    }
}
// ...
return {
    getAll: _getAll,
    add: _add,
    remove: _remove,
    findId: findId,

推荐阅读