Showing posts with label angular js. Show all posts
Showing posts with label angular js. Show all posts

Thursday, July 30, 2015

Adding pagination functionality in table using AngularJS

AngularJS (v 1.4) provides limito  filter to create a new array with specified number of elements out of existing array. It also supports from which index you want to create a new array.

you may read more about the filter from the following URL:

https://code.angularjs.org/1.4.0/docs/api/ng/filter/limitTo

Ex:

{{ sample_array | limitTo : numberofelement_perpage : startingrowno}}
where in my controller :

$scope.sample_array=["elem1","elem2","elem3","elem4","elem5","elem6","elem7","elem8","elem9","elem10"];
$scope.numberofelement_perpage=5;
$scope.startingrowno=0;
Using above filter we can create client side pagination in HTML table.


Following Plnukr shows the usage of limitito and begin to create client side pagination in HTML table.

http://plnkr.co/edit/tWFUPm?p=preview


Sunday, February 22, 2015

AngularJS controller in-depth

Controller in an AngularJS application plays as a role of bridge between your model and view. There are certain best practices need to be followed while designing a controller for your application.

  • REST calls should not be invoked in controller(Use Services).
  • Any filtering of the data like changing the text as browser language changes should not be done in controller(Use Filters)
  • Controllers should not be used to act as a communicator between two different AngularJS modules(Use event emitters)

Following example shows how to write a controller("Controller as" Syntax):

HTML file


<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="testapp">
      <div ng-controller="utilcontroller as util">
        <input type="text" ng-model="util.name">
        <button ng-click="util.greeting()">Say Hii</button>
        <div>
          <b>{{util.nameWithGreetingMsg}}</b>
        </div>

      </div>

    </div>
  </body>

Javascript file(script.js mentioned in the HTML file)


angular.module("testapp", []).controller('utilcontroller', function() {

    this.greeting = function() {
   
    this.nameWithGreetingMsg = "Hello " + this.name;
  }
});


Constructor is a constructor function, in the above example utilcontroller  is the constructor function, so when we mention the ng-controller directive in the HTML, this constructor function get called and a new object gets created. In the above example the new object name is util.

Let's write without "controller as" syntax.

HTML file

<!DOCTYPE html>
<html>

<head>
  <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body>
  <div ng-app="testapp">
    <div ng-controller="utilcontroller">
      <input type="text" ng-model="name">
      <button ng-click="greeting()">Say Hii</button>
      <div>
        <b>{{nameWithGreetingMsg}}</b>
      </div>

    </div>

  </div>
</body>
</html>

Javascript file(script.js mentioned in the HTML file)


angular.module("testapp", []).controller('utilcontroller', function($scope) {

  $scope.greeting = function() {

    $scope.nameWithGreetingMsg = "Hello " + $scope.name;
  }

});

In the above program all the properties and methods are attached to $scope.


Scope Inheritance

To achieve scope inheritance controllers can be used. It's very normal while creating an HTML view we assign different controllers at different view level. so for example in the below program:

<div ng-controller="vehiclecontroller as vehicle">
  <div ng-controller="carcontroller as car">
    No of wheels: <b>{{car.wheels}}</b>
    <button ng-click="vehicle.start()">Start Car</button>
  </div>
  <div ng-controller="bikecontroller as bike">
    No of wheels: <b>{{bike.wheels}}</b>
    <button ng-click="vehicle.start()">Start Bike</button>
  </div>
</div>

We have one parent controller and two child controllers,  the child controller are able to access the parent scope's data through referring the parent controller's name(vehicle.start). The 'start' method is defined in vehiclecontroller function:

angular.module("testapp", []).controller('vehiclecontroller', function() {
  
  this.start = function() {

    console.log("vehicle started");
  }

}).controller('carcontroller', function() {
  this.wheels = 4;
}).controller('bikecontroller', function() {
  this.wheels = 2;
});

$watch in controller

$watch is AngularJS is used to observe the scope properties, once the property's value  changes the callback function defined in the $watch executes.

Inside controller we write watchers(in case of controller as syntax):

controller('carcontroller', function($scope) {
  this.wheels = 4;
  this.gear="neutral";
  $scope.$watch(angular.bind(this,function(){return this.gear}),function(newVal,oldVal){
    console.log("Gear changed from"+ oldVal+ "to "+newVal);
  });


Note: More about angular.bind.


In case of without controller as syntax the $watch is pretty simple:
 $scope.$watch('gear',function(newVal,oldVal){
    console.log("Gear changed from"+ oldVal+ "to "+newVal);
  });


That's pretty much for controller's role inAngularJS app. In the upcoming post we will learn about FormController role in an application.



Tuesday, June 3, 2014

dragging table rows in IE9

This post is regarding the rearranging rows in a table by doing drag and drop( my previous post Rearranging rows by Drag and Drop ). The reason it does not work in IE 9(Internet explorer version 9 ) is  because of  the browser does not support draggable attribute for tr tag. So to make the reordering work by drag and drop  is by adding and 'a' tag as the first column which basically work as an anchor to do drag the row.

As IE9 support draggable attribute on 'a' then the browser can understand and do drag the particular row.
In the help of polyphil we can detect the browser and add draggable attribute to the correct tag:

1: IE9:  a tag (<a draggable="true"></a>)
1: Other browsers(Chrome and Firefox):  tr tag(<tr draggable="true">)

Please check this fiddle to know how to check the browser version and accordingly make the required element draggable.

JSFiddle example

Sunday, December 15, 2013

Example of Scope hierarchy in Angular JS

Angular JS has a $scope object which is mostly used to expose the domain model, model or domain level properties are assigned to $scope and can be accessed in view as well as controller.

In a single application we can have multiple controllers and each controller can refer to their own instance of $scope object, parent scope as well as $rootScope which is parent most scope of an application.$rootScope is created when the application bootstraps.

In the below example I have 2 controllers for my application, one is mainController and other one is named as subController. subController has a child controller named as childController.

MainController has one property named as Mngname, and it assigns a property named compayName  to it's parent scope which is nothing but $rootscope.

SubController has one property named as amName.

ChildController takes $rootScope along with $scope as it's inputs, it has one property named empName, it creates and assigns  one property of it's parent scope i:e, scope of SubController named as dept. It creates another property comp which holds the value of companyName property of $rootScope.


<!DOCTYPE html>
<html>
<head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/
angular.js"></script>
    <script>
        var  mainController=function($scope)
        {
            $scope.MngName='Jade';
            $scope.$parent.companyName='Mcommerce';
        }
        var  subController=function($scope)
        {
            $scope.amName='Jane';
            $scope.childController=function($scope,$rootScope)
            {
                $scope.empName='Scott';
                $scope.$parent.dept='Sales';
                $scope.comp=$rootScope.companyName;

            }

        }
         var appVar=angular.module('ScopeTest',[]);
    </script>
    <title>Angular scope chain</title>
</head>
<body ng-app="ScopeTest">
   <div ng-controller="mainController">
        <table title="ParentTable" border="5px">
            <caption>Level1</caption>
            <tr>
                <td>Manager Name</td>
            <td>{{MngName}}</td>
            </tr>
            <tr>
                <td>
            <table border="4px" ng-controller="subController">
                <caption>Level2</caption>
                <tr>
                <td>Associate Manager Name</td>
                   <td>{{amName}}</td>
                </tr>
                <tr>
                    <td>Reporting To</td>
                    <td>{{MngName}}</td>
                    </tr>
                <tr>
                    <td>Department Assigned at Employee Level</td>
                    <td>{{dept}}</td>
                </tr>
                <tr>

                    <td>

                        <table border="3px" ng-controller="childController">
                            <caption>Level3</caption>
                            <tr>
                                <td>Employee Name</td>
                                <td>{{empName}}</td>
                            </tr>
                            <tr>
                                <td>Comp</td>
                                <td>{{comp}}</td>
                            </tr>


                        </table>
                    </td>
                </tr>
            </table>
                </td>
            </tr>
        </table>

   </div>

</body>
</html>