Google+
Showing posts with label Modules. Show all posts
Showing posts with label Modules. Show all posts

Saturday, November 23, 2013

16 Step By Step How to add Searching functionality to an AngularJS Grid

         By Carmel Schvartzman


In this tutorial we'll learn how to add Searching functionality to an AngularJS Grid in our AngularJS Blogs Application. You can follow this step by step tutorial as a standalone, or you can learn all the posts to build the entire app. This is the STEP 16 in this walkthrough to design an end-to-end AngularJS client MVC app.

In the MODEL-VIEW-CONTROLLER architecture of AngularJS, a VIEW is related to a MODEL in the configuration step of the AngularJS app live, when we link it to some CONTROLLER.
This linking action is set in a Module, with the function of declaratively set how the AngularJS app should be bootstrapped. It's like the Main method in a program, setting the instantiation of the application.
Because AngularJS is based on the Dependency Injection paradigm, here is the place to configure the app injecting the functionality that we'll need.

We'll search an AngularJS grid, resulting in the following final appeareance of our AngularJS grid:


In order to add the searching functionality, find in your REST API the method which serves GET http requests. In the Rest API walkthrough we called that method "Blogs" in the "NatureController" controller:



Once you have found it, append to its parameters another new named "searchText" (the search term to be found) :



Find the code which populates the collection with the posts:



And change it to search for the requested text:


We need to check whether the search string has been initialized: in case it wasn't, just load all the posts:


Save, build and CTL-F5 to start the web server and browse to the Index.htm page:

No bugs. Now open the Blog.js file where the AngularJS Model is coded, and find the ajax call to the REST API :




Here we want to append the search argument, being careful to respect the case-sensitive name of the parameter "searchText":


We'll need a $scope variable to hold the search string, called "SearchText":



Now in the AngularJS View we add an input tag to enter that search string:


Save and refresh:


After that, we'll need also a button to trigger the search:



Save and refresh to see how it looks like:



Now we'll code the Search() javascript function called by the button. Find the function which performs the ajax call to the web server:



Enclose that function inside the new search function:


Be careful to type that function inside the boundaries of the AngularJS Controller:




The Controller starts at the following point:



Add to the Search() function the necessary instantiation we had before, and finally call the Search() function to initially populate the grid:



Save and refresh, and type some text in the search box:


It works: we got the posts which contain the text:




Now we need a button to reset the search:



Refresh and do another search:



Press "Reset" to cancel the search:

Now we need to disable the button if there is not a text for search:





Then when there is no text, the reset button is disabled:


Finally, let's add some style using the Twitter Bootstrap we added in a previous tutorial:




And this is the final appeareance of our AngularJS grid:






That's all!! 
Happy programming.....


כתב: כרמל שוורצמן

Monday, September 30, 2013

11 Step-By-Step How-to create an AngularJS Resource

         By Carmel Schvartzman

In this tutorial we'll learn  how to create an AngularJS Resource, which will allows us to fetch data on the client side, using a RESTful API in our AngularJS Blogs Application.You can follow this step by step tutorial as a standalone, or you can learn all the posts to build the entire app. This is the STEP 11 in this walkthrough to design an end-to-end AngularJS client MVC app.

In the MODEL-VIEW-CONTROLLER architecture of AngularJS, a VIEW is related to a MODEL in the configuration step of the AngularJS app live, when we link it to some CONTROLLER.
This linking action is set in a Module, with the function of declaratively set how the AngularJS app should be bootstrapped. It's like the Main method in a program, setting the instantiation of the application.
Because AngularJS is based on the Dependency Injection paradigm, here is the place to configure the app injecting the functionality that we'll need.

In the Module, will must install an AngularJS ngResource, that supports RESTful server-side interactions and  provides the $resource service, which we'll use afterwards.
This $resource service provides high level methods and therefore allows us to interact with the REST back end, without using the low-level $http service.


1. First, check that you have added the ngResource in the Module declaration, as we made in this tutorial.

2. Now, open the .js file which holds your javascript code ("Blog.js") and find the Controller that we linked to the View. This Controller will fetch the REST data from the back-end, and send it to the View.
Take a look at the snapshot: it states that when '/' do link the "BlogListCtl" controller to the "BlogList.html" View:

Therefore, we declare inside the BlogListCtl controller, a $scope attribute called "blogList", that will holds the posts list, from the BlogsResource service. To instantiate this service, we must create the above Factory, naming it exactly the same way we will at the Controller. Then we'll call the query() service's method. This method will make a request to the RESTful API of our application.

3. Let's write the REST action that reacts to the GET request made by our $resource service. Maybe you have already done that following the previous   How to create an REST API back-end      tutorial. If so, you can skip the next few steps. Elsewhere, create an empty MVC controller named "Nature":



4. We automatically get an Index method:

Instead  of an ActionResult return ,we need  JSON formatted data, so change the return type:


5. Add a Json object as the returned value:

Notice that the Json object returns the data its get, in a JSON notation format.

6. Because we want the method to response to GET requests, the second parameter must be GET allowed:

Therefore, pick "AllowGet" from the list:

Next, replace the "new {}" instance with the data you want to return from the method. For the complete walkthrough to do that, see the previous How to create an REST API back-end  tutorial.

7. Assuming you already built the RESTful back-end, get back to the Blog.js file, to continue with the creation of our $resource service. To see how to use an $resource service, let's take a look at the AngularJS.org's API documentation, at http://docs.angularjs.org/api/ngResource.$resource :


As you can see, the $resource uses an URL containing ":xxxx" parameters, that holds the arguments we send to the REST server. Those ":xxxx" parameters are defined in the same $resource instantiation, so let's code it the same way:


This code means that the call to the REST method includes the "Id" of the requested blog's post. Remember that if we don't send an Id, that means we want to fetch the ENTIRE list of posts.

Also, you'll notice that in the documentation example, there exists the declaration of an "charge" method, responding to a POST request. It is because the $resource service allows you to define your custom action as you wish:


The $resource service includes methods for all CRUD operations (Create Retrieve Update Delete) as the default set:


In the meantime we'll make use of the "query()" method to fetch all the posts. Also, add to the $resource service an "update" custom method, for the POST request, that we'll be using afterwards ("isArray" means that the returned data will be in an array disposition):

Rebuild the application, and open the "Index.html" in the browser (http://localhost:XXXXX/Index.htm#/):

Open the Developer Resources (F12) and at the "Network" tab select the Blogs.js script. There you can see the request made by our $resource service , and the 200 status code.

The AngularJS View should look like the following table:




That's all!! 
Happy programming.....


כתב: כרמל שוורצמן

Sunday, September 22, 2013

8 Step-By-Step How-to create an AngularJS View template

         By Carmel Schvartzman


In this tutorial we'll learn How-to create an AngularJS View , that will render the data fetched by a Controller in our AngularJS Blogs Application.  You can follow this step by step tutorial as a standalone, or you can learn all the posts to build the entire app. This is the STEP 8 in this walkthrough to design an end-to-end AngularJS client MVC app.

In the MODEL-VIEW-CONTROLLER architecture of AngularJS, a VIEW is related to a MODEL in the configuration step of the AngularJS app live, when we link it to some CONTROLLER.
This linking action is set in a Module, with the function of declaratively set how the AngularJS app should be bootstrapped. It's like the Main method in a program, setting the instantiation of the application.
Because AngularJS is based on the Dependency Injection paradigm, here is the place to configure the app injecting the functionality that we'll need.

An AngularJS View is just what the user sees, nothing more. Is an html template merged to the Data Model in a Module, and rendered to the Dom.

1. Open the Add New Item dialog, and select HTML page. Name it "BlogsList.html". That will be the View template embedded in our Index.html file:

2. Go back to the Index.html file we added in previous tutorials, and check that the ng-view directive appears inside a div tag. The template View we're designing right now will be embedded in that place:
You can indistinctly write "data-ng-view" or "ng-view".

3. Return to the BlogsList file and insert inside the body tags the following:


4. Next, add the "{{ }}" AngularJS notation, meaning that the AngularJS engine will react at what you write between this as some attribute or object belonging to the data SCOPE:


5. Write between the parenthesis the following attribute, that will contain the Title of our Blog:


6. Finally, delete all the standard html markup of the BlogsList.html file. We don't need it because this View is just a template embedded inside the Index.html file. The markup must remains as follows:


We have just designed our first AngularJS View. In the next Tutorial we'll create the corresponding  Controller as the Data Model for this View.

That's all!! 
Happy programming.....


כתב: כרמל שוורצמן

9 Step-By-Step How-to add an AngularJS Controller

         By Carmel Schvartzman


In this tutorial we'll learn How-to create an AngularJS Controller, in order to display data on a View. In the previous tutorial we linked the View that we just designed,  with a Controller using a Module  in our AngularJS Blogs Application.  You can follow this step by step tutorial as a standalone, or you can learn all the posts to build the entire app. This is the STEP 9 in this walkthrough to design an end-to-end AngularJS client MVC app.

In the MODEL-VIEW-CONTROLLER architecture of AngularJS, a VIEW is related to a MODEL in the configuration step of the AngularJS app live, when we link it to some CONTROLLER.
This linking action is set in a Module, with the function of declaratively set how the AngularJS app should be bootstrapped. It's like the Main method in a program, setting the instantiation of the application.
Because AngularJS is based on the Dependency Injection paradigm, here is the place to configure the app injecting the functionality that we'll need.

In short, a Controller is the Javascript code that stands behind a View; its functionality is to build the data MODEL and to publish it to the HTML template we call View. That is attained using the $scope directive.


  1. Go to the .JS file, where we just added a AngularJS Module, and create a Javascript var as follows: 



          Call your Controller as you wish, but as a convention we'll prefer an "Ctl" appended to the name. Notice that we must respect the previous Configuration declaration that stated the name of the controller (see the snapshot).
2. Our Controller will get an "$scope" parameter that we'll use to connect data Model to View:



3. In the previous tutorial, we added a View to our application, named "BlogList.html". In that view, we publish the title of the blog using the standard AngularJS "{{ }}" notation, as follows:

And now, at the Controller, we'll initialize that variable with a string:



Notice that we just added an attribute to the $scope. That's all. And the $scope will be used at the View.

4. Let's see how it looks like. Right-click the Index.html and press "View in Browser" (Notice we won't select the BlogList.html file, because it's just a template):


You will see the Blog Title in the rendered html . That was a perfect separation between Data Model and View presentation markup.


That's all!! 
Happy programming.....


כתב: כרמל שוורצמן