javascript - 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');
};
});
我认为错误是我无法调用工厂内的函数,我还想问我是否应该使用服务而不是工厂.....这是我所拥有的所有细节,请接受这个问题。.为什么我不能发布这个问题是我的重要疑问......请接受......
解决方案
如果调用上下文是可靠的,你可以参考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,
推荐阅读
- api - LinkedIn V2 api 完整档案
- c# - 如果使用 Angularjs ng-repeat,则 JQuery 无法处理输入标签
- c# - 检查实体是否已添加到 CrmServiceContext(保存之前)
- flutter - Flutter:如何避免 ListView 动态滚动(或更改其物理特性)
- javascript - 每次重新加载页面都会堆积内存 - 源文件多次加载到内存中
- c# - 如何超时执行功能
- here-api - 当 Here Map 上的默认 PositionIndicator 为绿色或灰色时。颜色取决于什么?
- python - 尽管级别是一致的,为什么 python 期望我的 elif 语句缩进?
- twitter-bootstrap - Bootstrap 列在折叠时垂直堆叠
- html - 如何在 VS 代码拆分属性中阻止 Prettier 到多行?