Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Last active August 29, 2015 14:06
Show Gist options
  • Save hectorgool/55e7f67f4ca4056bc0e1 to your computer and use it in GitHub Desktop.
Save hectorgool/55e7f67f4ca4056bc0e1 to your computer and use it in GitHub Desktop.
AngularJs - Changing Views with Routes
(function(){
'use strict';
var app, dependencies;
dependencies = ['ngResource', 'ngRoute','myApp.services','myApp.controllers'];
app = angular.module('myApp', dependencies);
app.config( function( $routeProvider ){
$routeProvider.when('/', {
templateUrl: 'partials/index.html',
controller: 'AppController'
});
$routeProvider.otherwise({
templateUrl:'app/partials/404.html'
});
});
})();
(function(){
'use strict';
var app = angular.module('myApp.controllers', []);
app.controller('PostsCtrl', ['$scope', 'PostsFactory',
function ($scope, PostsFactory, $location) {
$scope.posts = null;
PostsFactory.get(function(res){
$scope.posts = res;
});
}
]);
app.controller( 'PostCtrl', ['$scope', '$routeParams', 'PostFactory',
function ( $scope, $routeParams, PostFactory ) {
$scope.post = null;
PostFactory.query( { id: $routeParams.id }, function( res ){
$scope.post = res;
});
}
]);
})();
angular.module('myApp.services', ['ngResource']).
factory("geoProvider", function($resource) {
return {
states: $resource('../data/states.json', {}, {
query: { method: 'GET', params: {}, isArray: false }
}),
countries: $resource('../data/countries.json', {}, {
query: { method: 'GET', params: {}, isArray: false }
})
};
});
(function(){
'use strict';
var app = angular.module('myApp.services', []);
app.factory('PostsFactory', function ($resource, CSRF_TOKEN) {
return $resource('/api/posts', { 'csrf_token' :CSRF_TOKEN }, {
//GET, empty, Returns all posts
query: { method: 'GET' },
//POST, JSON String Create new post
save: { method: 'POST', isArray: true }
})
});
app.factory('PostFactory', function ($resource, CSRF_TOKEN) {
return $resource('/api/post/:id', { 'csrf_token' :CSRF_TOKEN }, {
//GET, empty, Returns single post
get: { method: 'GET'},
//PUT, JSON String, Updates existing post
update : { method : 'PUT', isArray: true },
//DELETE, empty, Deletes existing post
remove : { method : 'DELETE' }
})
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment