Javascript 在本地Json文件中添加/更新项| |分离控制器和服务文件
我是angular的新手,正在尝试构建一个简单的待办事项列表应用程序。我使用Ionic框架,并使用yeoman服务器进行测试。我已经设法加载了一个本地json文件,但是我搜索了一下,找不到如何添加/更新它。如果您能给我一些指导,我将不胜感激 我想做的第二件事是分离控制器和服务。目前所有服务都在controllers.js文件中。 这两项任务对于一个有经验的人来说都应该很容易,所以我想我会把它留给你们:P app.js:Javascript 在本地Json文件中添加/更新项| |分离控制器和服务文件,javascript,json,angularjs,yeoman,ionic-framework,Javascript,Json,Angularjs,Yeoman,Ionic Framework,我是angular的新手,正在尝试构建一个简单的待办事项列表应用程序。我使用Ionic框架,并使用yeoman服务器进行测试。我已经设法加载了一个本地json文件,但是我搜索了一下,找不到如何添加/更新它。如果您能给我一些指导,我将不胜感激 我想做的第二件事是分离控制器和服务。目前所有服务都在controllers.js文件中。 这两项任务对于一个有经验的人来说都应该很容易,所以我想我会把它留给你们:P app.js:angular.module('Todo',['ionic','Todo.co
angular.module('Todo',['ionic','Todo.controllers'))
Playlist.js(使用应用程序时获得的第一页):
我会一次回答你的两个问题
.controller('PlaylistsCtrl', function($scope , GetPlaylists) {
$scope.playlist =[];
$scope.playlist = GetPlaylists.getPlaylists(scope);
// below is a function that you can use for example with buttons ng-click="addNewPlayer('Michel Jackson')"
$scope.addNewPlayer = function(newPlayer){
$scope.playlist.push(newPlayer)
}
})
// To seperate your servises(which is a factory here) , cut and paste this factory in a new js file and include that js file in your index.html
.factory('GetPlaylists',function($http){
return{
getPlaylists:function(scope){
var promise = $http.get('jsonfile.json');
promise.then(function(data){
console.log(data);
return data
// I dont know about your json file , maybe you should write data.data or data[0] here
//Just console.log(data) it , to find the correct answer
});
}}
});
.controller('PlaylistsCtrl', function($scope, $http) {
$http.get('jsonfile.json').success(function(data){
console.log(data);
$scope.playlists = data;
});
})
.controller('PlaylistsCtrl', function($scope , GetPlaylists) {
$scope.playlist =[];
$scope.playlist = GetPlaylists.getPlaylists(scope);
// below is a function that you can use for example with buttons ng-click="addNewPlayer('Michel Jackson')"
$scope.addNewPlayer = function(newPlayer){
$scope.playlist.push(newPlayer)
}
})
// To seperate your servises(which is a factory here) , cut and paste this factory in a new js file and include that js file in your index.html
.factory('GetPlaylists',function($http){
return{
getPlaylists:function(scope){
var promise = $http.get('jsonfile.json');
promise.then(function(data){
console.log(data);
return data
// I dont know about your json file , maybe you should write data.data or data[0] here
//Just console.log(data) it , to find the correct answer
});
}}
});