Welcome to my first AngularJS learning module, wherein, i have done some, not so perfect, beginner application which lists the Github users and their corresponding profile details with repositories owned, extended to list the contributors too.
Some of my salient learning points to note about AngularJS:
- ng-app, ng-controller => directives are the most basic directives which angular monitors to work with in dealing with view.
- For Images with loading delays, use ng-src than src attribute. Angular does not display image till source is loaded using ng-src directive.
- Angular Philosophy => Throws error to console, than breaking page.
- ng-repeat => directive to use for repeating items from collection.
- Filters can be specified inside ng-repeat => ng-repeat=”item in collection | orderBy:<attributeName>” => + or – sign tells order.
- ng-include => fill in an HTML template.
- ng-show => show r hide elements in view
- Service or controllers should not directly talk to View in MVW model. Directives should.
- Try avoiding native JS functions over Angular viz. timers, etc. Helps in better binding and unit testable.
- Routing => done via ngRoute dependency module. Goes along with ng-view a placeholder to insert views.
In this application, i have used Angular Routing mechanism to route the pages as per need. This routing information should be the first JS script the angular should look and understand the whole structure.
Please feel free to play with the application: http://plnkr.co/edit/aZ86hP6BnrYGmD68PSfL
Below are the explanation about few code excerpts:
In the above code, the ng-app directive is very important, this informs the Angular framework to load itself and attach to this webpage. The script section should have angular js as the first item followed by the routing script. The rest scripts order does not matter belonging to within the application. Inside the body, i have added ng-view directive, this directive informs the angular to look up the routing configuration and insert the corresponding view/html file based on the URL of the browser.
app.js => Routing configuration
In the above code,at line 2, invoking angular.module API, configures the angular framework with the details required for this application to work. In this case, the first argument is the name of the application, can be anything, but should be same across the application. Second argument is the dependencies on which this application via angular dependent on.
On line 4, angular is instructed to configure the application with the routing options mentioned inside it. .when function describes the scenario and action (html and associated controller). .otherwise is the last fall back choice.
at line 2, i am getting the reference to the already loaded angular module for my application. The controller code goes inside a method with a name, here at line 4. Any data which needs to be transferred between view and controller is done via $scope service. The $location service (helper methods in .NET world) is used to get the URL parameters which browser is operating over.
At line 20, i am describing the controller information. The first argument is a string which has to match between the view and corresponding controller. Here, multiple views can point and communicate talk to a controller.
Above html file is a bit advanced and required for my requirement, got it resolved with help from Stackoverflow people. Wherein, upon user profile link is clicked, the web displays profile details along with repositories owned. On clicking over icon of the owned repository item, it expands to display the contributors list. The template to display for any repository is same, hence i wanted a static simple solution but only data change controlled from Model.
On line 4, the loop is iterated over the repository owned. Under each repository owned, a image is set based on the click action. This is decided by a bool state variable showContributors, its configured to be monitored under ng-Show directive. ng-Click is used to wire the eventing system. Hence these attributes decide to act upon the model data available.