Sunday, 20 March 2016

Priority option in the custom directive in angular js

In this post we are going to see the priority option in the angular directive, why we need the priority option in directive, it is because we can use the more than one directive in the single element, Now we see the example for this 


Expected Output:



To check for this option we will create a directive , on that directive we will use another two directive which will change the font and color of the directive. Now we will see the code


HTML :
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="application/javascript" src="angular.min.js"></script>
    <script type="application/javascript" src="mod.js"></script>
</head>
<body ng-app="app">
<div ng-controller="mainController">
    <hello-world red-font medium-size></hello-world>
</div>
</body>
</html>

From the above you can see the hello-world directive have two directives on the same element which will change the font size and color of the element


Directive:
var app = angular.module('app',[]);

app.controller('mainController',['$scope',function($scope){

}]);


app.directive('helloWorld', function () {
    return{
        restrict:'E',
        scope:{

        },
        template:'<div>Hello world {{Name}} . By Rajesh G , Architect</div>',
        controller: function ($scope) {
            $scope.Name = "Angular";
        },
        link: function (scope,element,attrs) {

        }
    }
});

app.directive('mediumSize', function () {
   return{
       restrict:'A',
       priority:10,
       compile: function (scope,element) {
           console.log('mediumSize called in compile Function');

           return function (scope, element, attrs) {
               element.css('font-size','20px');
               console.log('mediumSize called in Link Function');
           }
       }
   }
});

app.directive('redFont', function () {
    return{
        restrict:'A',
        priority:20,
        compile: function () {
            console.log('RedFont called in compile function');

            return  function (scope,element,attrs) {
                element.css('color','red');
                console.log('RedFont called in Link function');
            }
        }
    }
});


Log :
From the code you can see the redFont directive have more priority than mediumSize priority, so it will execute first, so compile function of redfont execute first and Link function of that directive execute last.




Output:





From this post you can see how to use the priority option in directive.

Delete the duplicate records from the Table SQL SERVER

In this post we are going to see how to delete the duplicate records from the table using SQL SERVER. For this we are going to create a employee table and insert a duplicate records.

CREATE TABLE EMPLOYEE(
     ID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
     FIRSTNAME VARCHAR(50),
     LASTNAME VARCHAR(50),
     DESIGNATION VARCHAR(50),
     PHONE INT,
     DEPARTMENTID VARCHAR(30)
)





/************** Insertion of the records ************/
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAJESH','G','ARCHITECT',1231232,'D1'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'SURESH','G','DEVELOPER',342232,'D1'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAMU','F','QA',124232,'D2'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'SHINY','E','HR',1231232,'D3'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'KRISHNA','G','QA',2343,'D2'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAMU','Y','QA',124232,'D3'

INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'SURESH','G','DEVELOPER',342232,'D1'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAMU','F','QA',124232,'D2'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'SHINY','E','HR',1231232,'D3'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'KRISHNA','G','QA',2343,'D2'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAMU','Y','QA',124232,'D3'

INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAJESH','G','ARCHITECT',1231232,'D1'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'SURESH','G','DEVELOPER',342232,'D1'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAMU','F','QA',124232,'D2'
INSERT INTO EMPLOYEE (FIRSTNAME,LASTNAME,DESIGNATION,PHONE,DEPARTMENTID)
SELECT 'RAMU','Y','QA',124232,'D2'



From the above records inserted we want to delete the duplicate records, other than identity column everything needs to be unique, so we are going  to partition the table in row_number over the columns other an identityor primary key

      SELECT 'ACTUAL RECORDS'
      SELECT * FROM EMPLOYEE


     ;WITH EMP AS
     (
          SELECT *, ROW_NUMBER() 
          OVER(PARTITION BY FIRSTNAME,
                            DESIGNATION,
                            PHONE,
                            LASTNAME,
                            DEPARTMENTID 
                   ORDER BY DEPARTMENTID) AS ROWNUMBER 
           FROM EMPLOYEE
      )

    DELETE FROM EMP WHERE ROWNUMBER > 1

    SELECT 'AFTER DELETE DUPLICATE RECORDS'
    SELECT * FROM EMPLOYEE










From this post you can learn how to delete the duplicate records from the Table



Sunday, 13 March 2016

link function in Angular JS Directive

In this post i am going to discuss about the concepts of link function in angular Directive , normally the logic of directive is resides in the link function, which have the following parameters scope,element,attrs,controller , Now we see the example of the custom directive 

link:function Linkfn(scope,elem,attrs,ctrl){}

scope: the scope of the directive
elem: Dom element where the directive applied
attrs: collection of attrs
ctrl : array of controllers


Directive :
*************************



Controller:
*************************

appRoot.controller('MainController', function ($scope) {
    'use strict';   
     $scope.Product = {};
    
     $scope.sourceList = [{"id":1,"name":"Apple"},
                          {"id":2,"name":"Orange"},
                          {"id":3,"name":"Banana"},
                          {"id":4,"name":"Papaya"},
                          {"id":5,"name":"Jackfruit"}];
    $scope.Product.Selected = $scope.sourceList[0];
    
});



Html:
*************
Use the track-change directive in tag that you implement the ng-model, in this example i am used in select , where whenever user selects the new value, old value is retained in OldValue property, you can see the Model that maintains the history, but in view when we bind it shows only the latest value ,This is because of $formatters.






Output:
*******************



appRoot.directive('trackChange',function(){
    return{
        restrict:'A',
        require:'ngModel',
        link:function(scope,element,attrs,ngModel){
            
            var historyObject = {NewValue:undefined,OldValue:undefined};
            var OldValue = {};
            var NewValue = {};
            ngModel.$formatters.push(function(value){
                if(historyObject.NewValue!=undefined)
                    return historyObject.NewValue;
                if(value!=undefined){
                    historyObject.NewValue = value;
                }
                return value;
            });
            
            ngModel.$parsers.push(function(value){             
                
                historyObject.OldValue = historyObject.NewValue;;
                historyObject.NewValue =  value;
                ngModel.$setViewValue(historyObject.NewValue);
                ngModel.$render();
                ngModel.$setValidity('is_valid',true);
                return historyObject;
            });
            
        }
    }
})
var appRoot = angular.module('appRoot',[]);
appRoot.controller('MainController', function ($scope) {
    'use strict';   
     $scope.Product = {};
    
     $scope.sourceList = [{"id":1,"name":"Apple"},
                          {"id":2,"name":"Orange"},
                          {"id":3,"name":"Banana"},
                          {"id":4,"name":"Papaya"},
                          {"id":5,"name":"Jackfruit"}];
    $scope.Product.Selected = $scope.sourceList[0];
    
});
<div ng-app="appRoot">
    <div style="margin-left:40px;" ng-controller="MainController">
      
        <br />
        <select ng-change="modelChange()" track-change="" ng-model="Product.Selected" 
                ng-options="prod.name for prod in sourceList" >
        </select>  
        <span style="color:orange">[[Product.Selected]]</span>
</div>
</div>
From this post you can learn how to create a Directive with link function.