我已经从http://www.tutorialspoint.com/angularjs/下载了AngularJS - ToDo应用程序,并试图实现相同的功能。由于我是AngularJS的新手,我被困在某个地方。
在这个应用程序AngularJS服务" localStorage的"包含在对象名称' store'在控制器中。但是,当我试图这样做时,它对我不起作用。
mainApp = angular.module('mainApp', ['ngRoute']);
mainApp.config(function($routeProvider) {
'use strict';
var routeConfig = {
templateUrl: 'template/add_todo.html',
controller: 'addToDoController',
resolve: {
store: function(todoStorage) {
// Get the correct module (API or localStorage).
return todoStorage.then(function(module) {
return module;
});
}
}
};
$routeProvider
.when('/', routeConfig)
.otherwise({
redirectTo: '/'
});
});
mainApp.factory('todoStorage', function($http, $injector) {
'use strict';
// Detect if an API backend is present. If so, return the API module, else
// hand off the localStorage adapter
return $http.get('/api')
.then(function() {
//return $injector.get('api');
}, function() {
var store = $injector.get('localStorage');
return store;
});
})
mainApp.factory('localStorage', function($q) {
'use strict';
var store = {
todos: [],
insert: function(todo) {
store.todos.push(todo);
console.log(store.todos);
return store.todos;
}
};
return store;
});
mainApp.controller('addToDoController', function($scope, store) {
'use strict';
$scope.addTodo = function() {
var newTodo = {
title: $scope.newTodo.trim(),
completed: false
};
$scope.saving = false;
store.insert(newTodo)
$scope.newTodo = '';
}
});