I use the following code which run OK with very simple Angular UI that run intro page with form and when you click on button it navigate to another simple form to create data. since this is my first angular I would like to get some feedback if this code is OK and if not how can I improve it...(structuring code etc...)
This is the controller.js
angular.module('blogCtrl',[])
.controller('BlogCtrl', blogClass);
function blogClass($scope,$routeParams,$window,$location,BlogService,BlogList){
$scope.blog={};
$scope.errors=null;
$scope.blogs=[];
$scope.getBlogs=function(){
BlogList.blogs(function(data){
if(data.status){
$scope.blogs=data.data;
}else{
$scope.errors=data.errors;
}
}, function(error){
$scope.errors=error;
});
};
$scope.getBlog=function(id){
if(id){
BlogService.read({id:id}, function(data){
if(data.status){
$scope.blog=data.data;
$scope.message=null;
$scope.error=null;
}else{
$scope.error=data.errors;
$scope.message=null;
}
}, function(error){
$scope.errors=error;
});
}else{
$scope.getBlogs();
}
};
$scope.getBlog($routeParams.id);
$scope.deleteBlog=function(id){
BlogService.remove({id:id}, function(data){
if(data.status){
$scope.message=data.message;
$scope.errors=null;
$scope.getBlogs();
}else{
$scope.errors=data.errors;
$scope.message=null;
}
}, function(error){
$scope.errors=error;
});
};
$scope.addBlog=function(){
BlogService.create($scope.blog,function(data){
if(data.status){
$scope.message=data.message;
$scope.blog={};
$scope.errors=null;
$location.path('/');
$location.replace();
}else{
$scope.errors=data.errors
}
}, function(error){
$scope.errors=error;
$scope.message=null;
});
};
$scope.updateBlog=function(id){
BlogService.update({id:id},$scope.blog,function(data){
if(data.status){
$scope.message=data.message;
$scope.errors=null;
}else{
$scope.message=null;
$scope.errors=data.errors;
}
}, function(error){
$scope.errors=error;
});
};
}
This is the main.js
angular.module('blog',['ngRoute','ngResource','blogService','blogCtrl'])
.config(function($routeProvider, $locationProvider,$httpProvider) {
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$routeProvider
.when('/',{
templateUrl:'/html/view-blogs.html'
,controller:'BlogCtrl'
})
.when('/blog/:id',{
templateUrl:'/html/view-blog.html',
controller:'BlogCtrl'
})
.when('/add',{
templateUrl:'/html/add-blog.html',
controller:'BlogCtrl'
})
.when('/update/:id',{
templateUrl:'/html/add-blog.html',
controller:'BlogCtrl'
})
.when('/delete/:id',{
templateUrl:'/html/delete-blog.html',
controller:'BlogCtrl'
})
.otherwise('/');
$locationProvider.html5Mode(true);
});
This is the services.js
angular.module('blogService',[])
.service('BlogService',function($resource){
return $resource('/blog/:id', null,
{
read:{method:'GET'},
update:{method:'PUT'},
create:{method:'POST'},
remove:{method:'DELETE'}
});
})
.service('BlogList',function($resource){
return $resource('/blog',null,{
blogs:{method:'GET'}
});
});
update:
Since I dont get any comments or answers if my code is great please tell too :)