我是角色的新手,我试图建立一个简单的待办事项列表应用程序。我使用Ionic框架并使用yeoman服务器测试它。我已经设法加载了一个本地json文件,但我搜索了一下,我无法找到如何添加项目/更新它。如果你能指导我如何做到这一点,我将不胜感激。
我想要做的第二件事是分离我的控制器和服务。目前所有服务都在controllers.js文件中。 对于那些有角度体验的人来说,这两项任务都应该很容易,所以我想我会把它留给你们:P
app.js:angular.module('Todo', ['ionic', 'Todo.controllers'])
Playlist.js(午餐时获得的第一页):
<ion-content class="has-header">
<ion-list>
<ion-item ng-repeat="playlist in playlists" href="#/app/playlists/{{playlist.id}}">
{{playlist.title}}
</ion-item>
</ion-list>
</ion-content>
控制器:
.controller('PlaylistsCtrl', function($scope, $http) {
$http.get('jsonfile.json').success(function(data){
console.log(data);
$scope.playlists = data;
});
})
答案 0 :(得分:1)
我会在一个回答你的问题;
.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
});
}}
});