Introduction
At 19/08/13 I've spoke at
Gil Fink UserGroup at Sela about introduction to Angular.js. Below a short overview of the example app presented.
Structure
When building Angular application is is most important to divide it into modules. The current example was relatively small so the modules division was made by different components of the framework (e.g. controller, services, filters, etc..).
Note, that if the application is large and complex the division to modules should be made by features rather than by components. Each module should then contain everything related to the feature. It will allow us to take the feature to another Angular project with ease.
Please note, there are 5 examples in the main directory from the most basic to the most complex, I'll overview the last as it includes all the other examples as well
Bootstrapping
As an entry point I have the application.js file which defines all the modules available for the projects including the dependencies between them.
window.directives = angular.module('directives', [], function () {
});
window.services = angular.module('services', [], function () {
});
window.filters = angular.module('filters', [], function () {
});
Right after that, the main module configures the routes.
window.application = angular.module('myMovies',
['directives', 'services', 'filters'], function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'mainCtrl',
templateUrl: 'templates/movies_list_view.html'
})
.when('/about', {
templateUrl: 'templates/about_view.html'
})
.when('/movieDetail/:id', {
controller: 'movieDetailController',
templateUrl: 'templates/movie_detail_view.html'
})
.otherwise({
templateUrl:'templates/404.html'
})
});
The $routeProvider has two functions. when - used to declare a valid route either with or without parameters and otherwise - will redirect at any illegal route entered.
Controllers
Two controllers are available one for displaying the movies on the main page and the other for displaying additional details about a movie
The mainController requests the mockService to get the movies data, and also defines event to remove movie on click.
The movieDetailController uses the same service to get movie by id. Note, the movie id is passed as a parameter in url (not as querystring). The controller then reads the parameter with the $routeParams provider.
Services
One service is available. the mockService gets it's data statically from another js file with timeout to simulate a delay.
Because I used timeout function there I need to call $scope.$apply() inside controller to notify Angular about changes to the data.
$scope.$apply(function () {
$scope.model = {
movie: movie
}
});
Normally, when fetching data from server we should use $http service to get the data, the $http service will take care for notifying about changes
Filters
Build-in filters are used in movies list template to filter movie results by the search field. Also, one custom filter is available to convert minutes to more visualisable format.
Templates
Templates for every page is available and are loaded when route gets resolved. Also 404 template is available for the 'otherwise' function.
Conclusion
To create this example I've simplified the structure of the project. The file structure should be a bit different and I hope that soon I'll post a full project example showing the structure as I see it.
I also simplified the code for the sake of this example, so probably it won't look 100% the same in real life project.
I'll be glad for every input you may have about this post or the examples.