March 11, 2015

Directives and Views in AngularJS

AngularJS views mix data from the model into an HTML template. You use AngularJS directives to tell AnguluarJS how to mix the data into the HTML template.

Here i am going to take an example to show you how this works. I am going to fetch the user details and their repo details respectively from the github api and sort them. I am also going to create a search box to search the user and display their details.

ng-model

What i am going to do is have an input where i can type in any username that i want, click a button, then it will go out and fetch the information for me. Instead of hardcoding that value i want to be able to input the value as a user.

What ng-model will do is as the user types into the input, ng-model will push the value of the input into my scope object. ng-model is an extremely useful directive because you can apply to input, select, textarea. What ng-model will do is always keep your data in synch. It's going to move information that the user types into your model.

ng-click

Now i want to know when the user is ready to search for a particular username and there are couple of ways to do that. One way to do is attach a directive ng-click to the input button. Typically the expression is going to be a function call.

<input type="submit" value="Find" ng-click="search()">

When someone clicks on the button angular goes out and finds that function that is defined in my model. It is attached to $scope and i want angular to invoke it. In this case it's a search function. Inside the search function i am going to write the code that gets the username, calls the github API and retrieves the information of that particular user.

When you write expressions for ng-click some of them can be rather fancy, you can even incude thigs like parameters to that method call. In this case i am going to call search and pass the parameter username. This is not necessary as scope will already have username as a property. Thanks to ng-model. But i am going to leave that parameter there just to see how easy it is to call search and pass something along.

ng-submit

In order to tell the browser to be able to tell the user, that this particular input is required, i wont be able to handle ng-click on this button event. Instead i will handle this in the form using the directive ng-submit. Use this directive to catch the submit event of the form and have that call search passing in the username

ng-repeat

The information we get from github includes repos_url. We are going to get some information's from the github like name of the repo, number of stars obtained for that repo and the language and view this in html.

Now we have display information's about each repository into html. Typically we will display information from an array inside of an unordered list or a table. I am going to go with table because i want to display multiple things about each repository.

I need a table row where i am going to display each repository. I am going to use the directive ng-repeat. Here i will write an expression like ng-repeat="repo in repos". The idea is like that ng-repeat is more likely a for loop.

ng-show & ng-hide

We can hide or show pieces of our markup very easily. The directives ng-show & ng-hide both useful directives when you want to make things appear and disappear from the screen based on some expression and some flag that is in the model.

ng-include

Directive ng-include is useful for number of scenarios. ng-include can bring html from another source like another file and you can use that html in the current view. This is useful because you can break up complex page into pieces and is also useful if you have markup that you can reuse in other views of the application. For example the values that we are displaying like name, gravatars, repository details all can be added in a separate html file and include this in the current html using the directive ng-include.

<div ng-include="'userdetails.html'"></div>

Conclusion:

All together i am going to show you an example. I am going to use API from github to get the user information and display it in html. The user has an option to search the user he wanted to see. In addition i am going to get the github API for repository and fetch details from it. Let's even sort the data's as user wanted and display it in html.