简体   繁体   English

Angularjs ReferenceError:未定义函数

[英]Angularjs ReferenceError: function is not defined

i am getting ReferenceError: findId is not defined this the code我收到ReferenceError: findId is not defined this the code

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');
    };
});

i think the error is i cant call a functions within the factory, also i wanted to ask whether should i use service instead of factory..... this is all the details i have please accept the question .我认为错误是我不能在工厂内调用函数,我还想问我是否应该使用服务而不是工厂......这是我所有的细节,请接受这个问题。 .why am i not able to post this question is my important doubt...please acccept..... .为什么我不能发布这个问题是我的重要疑问...请接受.....

If the calling context is reliable, you can refer to this to get the returned object, and from that, get to the findId property on the returned object:如果调用上下文是可靠的,您可以参考this来获取返回的对象,并从中获取返回对象的findId属性:

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

If the calling context isn't reliable, define findId as a standalone function beforehand:如果调用上下文不可靠,请事先将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,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM