Saturday, 30 September 2017

ng-hide & ng-show in angular JS



ng-hide / ng-show  in angular JS are directives that which can have the type as Boolean value resulting true or false. Based on this value the behavior of the element to which we used this attribute is going to be change.
Sample Look :
<span ng-hide="hdpin">{{pinnum}}</span>
<span ng-show ="hdpin">{{pinnum}}</span>  

Watchers in angular JS


In general, angular JS maintains the watchers for all of our scope object variables.That is the reason how the UI or DOM gets changed based on change of the scope variables immediately effecting other areas too wherever used(or binded).
A very well known concept in angular JS which is very interesting is two-way data binding, where the internal or behind the scenes the mechanism comes from the watchers itself.

Thursday, 28 September 2017

ng-if in angular JS


ng-if in angular JS :
In angular JS ng-if is used to create or destroy the element from the DOM tree structure of the HTML page.The ng-if directive expects an expression that which makes the result to be in true or false.

The ng-if Look :

<div ng-if="sal > 30000">
Ravi’s salary is 80000

</div>

Monday, 25 September 2017

Data Binding in angular JS


Data binding :
In angular JS, the term data binding meant that establishing the connection between the mark up page or HTML content like view and the controller or the scope.

Technically in angular JS it was defined as like the synchronization happening between the models and views. Here synchronization means like changes reflecting from one to another.

Saturday, 23 September 2017

Provider in angular JS

Provider in angular JS is a function whose instance can provide service that which can be able to even configure.The services of the provider can be accessed throughout the application.The provider function can return object or primitives or can be as function.The providers in angular JS are nothing but a constructor function.By using the provider  we can access the data in any controller whereever we required by just injecting the provider in controller function.A provider function must have a $get function,which makes statement as like provider is an object with a $get function.provider is the core layer for all the other singleton objects like services, factory, value and constant.

Tuesday, 19 September 2017

factory in angular JS

Factory in angular JS is a kind of service provided to the called one(can be controller).factories can creates objects and returns them (objects).Factories in angular JS can also return the primitives(like string, number or boolean).The factory can also have the dependencies in it's function.Factory in angular JS is considered to ba as a singleton object so it can be instantaited once per the module.
The factory in angular is just like creating the object and attaching the properties to the object and then accessing them in the called one which can be as controller.

Monday, 18 September 2017

service in angular JS

In angular JS the service is an singleton object, where we can inject the service reference name to the controller function. As it is singleton object it can be instantiated  once per the application and can be accessed throughout the application.

Look of custom service creation :
var app = angular.module('apptest', [])
        app.service('servicename', function () {
//return some data through function.
            }
        })
In the above example we can observe the service with it’s name ‘servicename’ in the first parameter and to the next parameter as an anonymous constructor function which can contain the  function that returns the data to the called one.

Example on service :
 var myapp = angular.module('myapp', []);
        myapp.service('actions', function () {
            this.add = function (val1) {
                return parseInt(val1) + parseInt(val1);
            }
            this.sub = function (val1) {
                return parseInt(val1) - parseInt(val1);;
            }
        });

In the above code I had just created the module named as "myapp" and then created a service to that app by using the service().'actions' is the anme of the service that which we created and within the constructor function of anaother parameter I created two functions as "add" and "sub"
In the above code if we observe, I used "this" keyword. The purpose of that keyword use is to refer that particular object. In javascript every function will internally treated as an object itsef.
Now, following is the code of controller with the dependency containing the 'actions' service :

 myapp.controller('myctrl', function ($scope, actions) {          
            $scope.GetResult = function () {              
                $scope.addres = 'Addition to itself is : ' + actions.add($scope.val1);
                $scope.subres = 'Substraction to itself is : ' + actions.sub($scope.val1);
            }
        });

In the above code we can find the controller injecting with the service 'actions' within the constructor function.

Now lets have look over the HTML code:
<body ng-app="myapp">
    <div ng-controller="myctrl">
        <input type="text" id="txtval" ng-model="val1" />
        <input type="button" value="Get Result" ng-click="GetResult()"/> <br />
        <span ng-bind="addres"></span><br />
        <span ng-bind="subres"></span>
    </div>
</body>

From the above code, the "GetResult()" function will be called whehever user clicks on the button.
within the GetResult() function we assigned the values of ng-bind="addres" and ng-bind="subres" by calling the services of add as "actions.add($scope.val1)" and services of sub as actions.sub($scope.val1).
Here, $scope.val1 is the vaalue that which comes from the input textbox where we used the ng-model.
Image refernce of above code :


Output :

In angular JS we are having some built-in services, where we can use based on the requirement. Some of the built-in services are as like, $filter , $location , $cookie ,.. etc.

Thursday, 14 September 2017

constant in angular JS

Constant in angular JS :
Constant is an object in angular JS.By using the constant we can make the required or re-useble values to be appear globally in application level even.Constant object can also act as like the dependency injection.In simple we can say as like the Constant in angular JS will acts as like service.

Look of constant :
 var app = angular.module('app', []);
     app.constant('valueis',20);

From the above we can observe that the module "app" is set to dbe registered with the "constant" object with holding the "valueis" variable with its value 20.
In constant object we can define any type of data like string, int, array, object.

Look of constant with object :
var app = angular.module('app', []);
 app.constant('user', {
            userid: 1,
            username: 'Hari',
            usersal: 20000
        });
 app.controller('appCtr2', function ($scope,user) {
            $scope.name= user.username;
        });

<div ng-controller="appCtr2">
      User Name is : {{name}}
    </div>

The constant object can be used like a configure file where we can register all our constant values related to the application.

The main thing that can make or advantegeous way with the constant is that, the constant values or its object cannot allowed to change in angular.We can register the constants in multiple controllers.

Differences between constant and value object :
constant's value can not be change where as value's data can be change.
constant can be injected any where where as value can be injected in controller, service, factory but can't be injected in config.

Wednesday, 13 September 2017

value object in angular JS

value in angular js :

Value in angular JS is an object. By using this object we can create or construct the values that which can be used in the dependent controllers.We can also say it as like "providing the service for the dependents" where the dependents tends to the controllers.

Look of value :
 var app = angular.module('app', []);
     app.value('valueis',20);

From the above we can observe that the module "app" is set to dbe registered with the "value" object with holding the "valueis" variable with its value 20.
In value object we can define any type of data like string, int, array, object.

Look of value with object :
var app = angular.module('app', []);
 app.value('user', {
            userid: 1,
            username: 'Hari',
            usersal: 20000
        });
 app.controller('appCtr2', function ($scope,user) {
            $scope.name= user.username;
        });

<div ng-controller="appCtr2">
      User Name is : {{name}}
    </div>

We can also access this value objects data in multiple controllers as it will instatiate by the time of angular js configuring within browser.
while equalizing any other object to the "Value" object, a direct mapping is not allowed as like follows :
Let us assume the same properties of "User" in "response" object.
    user = response     --> Cannot be applied.
    user.Id  = response.Id      --> can be applied.
    user.name= response.name  --> can be applied.

We can even change the value of the "Value" object which will not prompt any error and applies whereever we use, as the angularjs supports the two-way data binding.