AngularJs 新手我正在尝试通过构建单页 Todo 应用程序来进行训练。问题是,我在加载我试图用于此的 localStorage 工厂时遇到问题。目前我陷入以下错误:
Undefined is not a function at routeconfig.resolve.store on app.js line 11.
这是代码:
应用程序.js
angular.module('TodoAngular', ['ngRoute'])
.config(function($routeProvider) {
'use strict';
var routeConfig = {
controller: 'TodoController' ,
templareUrl: 'app/partials/TodoList.html',
resolve: {
store: function (todoAngularStorage) {
// Récupère le module qui gère le localStorage
return todoAngularStorage.then(function (module) {
module.get(); // Récupère les données
return module;
});
}
}
};
$routeProvider
.when('/todos', routeConfig)
.otherwise({
redirectTo: '/todos'
});
}
);
todoAngularStorage.js
angular.module('TodoAngular')
.factory('todoAngularStorage', function ($http,$injector) {
'use strict';
return $injector.get('localStorage');
})
.factory('localStorage', function($q) {
'use strict';
var STORAGE_ID = 'todoAngularLocalStorage';
var store = {
todos: [],
//récupérer depuis le local storage
_getFromLocalStorage: function(){
return JSON.parse(localStorage.getItem(STORAGE_ID) || '[]');
},
//enregistrer dans le local storage
_saveToLocalStorage: function (todos) {
localstorage.setItem(STORAGE_ID, JSON.stringify(todos));
},
//supprimer un Todo
delete: function (todo) {
//instanciation de l'API Deferred
var deferred = $q.defer();
store.todos.splice(store.todos.indexOf(todo), 1);
store._saveToLocalStorage(store.todos);
deferred.resolve(store.todos);
return deferred.promise;
},
//récupérer les todos
get: function () {
//instanciation de l'API Deferred
var deferred = $q.defer();
angular.copy(store._getFromLocalStorage(), store.todos);
deferred.resolve(store.todos);
return deferred.promise;
},
//ajouter un todo à la fin de la todo list
insert: function (todo) {
//instanciation de l'API Deferred
var deferred = $q.defer();
store.todos.push(todo);
store._saveToLocalStorage(store.todos);
deferred.resolve(store.todos);
return deferred.promise;
}
};
return store;
});
我一直在尝试调试它,看来问题来自我app.js
文件以下行中函数中传递的“模块”
return todoAngularStorage.then(function (module) {
在构建我的应用程序时,我一直在关注这个示例(https://github.com/tastejs/todomvc/tree/master/examples/angularjs),我现在真的不知道我的错误在哪里。