Angularjs ReferenceError: function is not defined
Angularjs ReferenceError: function is not defined
我得到了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');
};
});
我认为错误是我无法在工厂内调用函数,
我也想问我是否应该使用服务而不是工厂.....
这是我所有的细节,请接受这个问题。
.为什么我不能post这个问题是我的重要疑惑...请采纳...
如果调用上下文是可靠的,你可以参考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,
我得到了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');
};
});
我认为错误是我无法在工厂内调用函数, 我也想问我是否应该使用服务而不是工厂..... 这是我所有的细节,请接受这个问题。 .为什么我不能post这个问题是我的重要疑惑...请采纳...
如果调用上下文是可靠的,你可以参考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,