Sunday 28 January 2018

custom directive in angularjs

step 1 : Controller

var myapp = angular.module("myapp", []);

myapp.controller('EmployeeCtrl', function ($scope) {
    $scope.Hamid = {};
    $scope.Hamid.Fname = "Hamid";
    $scope.Hamid.Lname = "Ali";

    $scope.Noor = {};
    $scope.Noor.Fname = "Noorul Huda";
    $scope.Noor.Lname = "Khan";
});

step 2 : custom directive


myapp.directive('EmployeeDir', function () {
    var directive = {};
    directive.restrict = 'E';
    directive.template = "{{student.Fname}}{{student.Lname}}";

    directive.scope = {
        student: "=name"
    }

    directive.compile = function (element, attributes) {
        element.css("border", "2px solid #ccccdd");

        var linkFunction = function ($scope, element, attributes) {
            element.html("Student First Name: <b>" + $scope.student.Fname + "</b> , Last Name: <b>" + $scope.student.Lname + "</b><br/>");
            element.css("background-color", "#gg00cc");
        }
        return linkFunction;
    }

    return directive;
});


Step 3 : apply on html


            <div ng-controller="EmployeeCtrl">
                <student name="Hamid"></student><br />
                <student name="Noor"></student>
            </div>






AngularJS Life Cycle

AngularJS Life Cycle

Since you comprehend the segments engaged with an AngularJS application, you have to comprehend what occurs amid the life cycle, which has three stages: bootstrap, arrangement, and runtime. Understanding the life cycle of an AngularJS application makes it less demanding to see how to outline and actualize your code.
 
The three periods of the life cycle of an AngularJS application happen each time a site page is stacked in the program. The accompanying areas depict these periods of an AngularJS application.
 
1- Bootstrap
2- Compilation
3- Data Binding
 
1-Bootstrap 
 The primary period of the AngularJS life cycle is the bootstrap stage, which happens when the AngularJS JavaScript library is downloaded to the program. AngularJS instates its own vital segments and after that introduces your module, which the ng-application order focuses to. The module is stacked, and any conditions are infused into your module and made accessible to code inside the module.
 
2- Compilation
The second period of the AngularJS life cycle is the HTML aggregation arrange. At first when a page is stacked, a static type of the DOM is stacked in the program. Amid the aggregation stage, the static DOM is supplanted with a dynamic DOM that speaks to the AngularJS see.
This stage includes two sections: navigating the static DOM and gathering every one of the orders and after that connecting the orders to the suitable JavaScript usefulness in the AngularJS worked in library or custom mandate code. The orders are joined with an extension to create the dynamic or live view.
 
3- Data Binding
 
The last period of the AngularJS application is the runtime stage, which exists until the point when the client reloads or explores far from a site page. By then, any adjustments in the degree are reflected in the view, and any adjustments in the view are specifically refreshed in the extension, making the degree the single wellspring of information for the view.
AngularJS carries on uniquely in contrast to conventional strategies for restricting information. Conventional strategies join a format with information got from the motor and after that control the DOM each time the information changes. AngularJS arranges the DOM just once and afterward connects the aggregated layout as fundamental, making it considerably more proficient than customary techniques.

Thursday 25 January 2018

basic sql query

SELECT * from Customer  order by FirstName

SELECT * from Customer WHERE FirstName like '[a-a]%'  order by FirstName

SELect distinct city from Customer order by City


  SELECT DATEPART(YEAR, [OrderDate]) FROM [Order]
  SELECT COUNT(*) from Customer

  SELECT * from [dbo].[Order]  order by NEWID()
   SELECT top 5 customerId, sum(TotalAmount) from [order]
group by CustomerId
order by  sum(TotalAmount) desc

select 2 highest salary

SELECT Top 1 totalamount from
 ( select top 2 totalamount from [order] order by totalamount desc
 ) T
  order by TotalAmount



update table when male then female, when female then male



  update customer set Gender= Case WHEN gender='m' then 'f'
                                WHEN  gender='f' then 'm'
END 



Monday 15 January 2018

create duplicate table without data

--SELECT TOP 0 * INTO foo FROM customer

--select * into t2 from t1 where 1=0

Sunday 14 January 2018

moved in your folder using cmd

pushd F:\Study2018\Demo2018\Angular2Demo\Angular2Demo

factory method for error log implementation

There are four 4 thing need to keep in mind:

  1. Product
    This defines the interface of objects the factory method creates
  1. ConcreteProduct
    This is a class which implements the Product interface.
  1. Creator
    This is an abstract class and declares the factory method, which returns an object of type Product.
  1. ConcreteCreator
    This is a class which implements the Creator class and overrides the factory method to return an instance of a ConcreteProduct.


// Example  : LogError

  public  interface ILog   // 1-Product
    {
      void LogError();
    }

    class WordLog : ILog     // 2- ConcreteProduct
    {
        public void LogError()
        {
            Console.WriteLine("log error in word");
        }
    }

    class CsvLog : ILog  // 2- ConcreteProduct
    {
        public void LogError()
        {
            Console.WriteLine("log error in csv");
        }
    }

    class SqlLog : ILog   // 2- ConcreteProduct
    {
        public void LogError()
        {
            Console.WriteLine("log error in sql");
        }
    }
   

   public abstract class LogFactory     //3- Creator
    {
        public abstract ILog LogCommand(string logType);
    }

    class ConcreteLog : LogFactory  // ConcreteCreator
    {
        public override ILog LogCommand(string logType)
        {
            switch (logType)
            {
                case "word":
                    return new WordLog();                 
                case "csv":
                    return new CsvLog();                 

                case "sql":
                    return new SqlLog();
                default:
                    throw new ApplicationException(string.Format("logType '{0}' cannot be created", logType));

            }
        }
    }

    public class Client
    {
        public void Test()
        {
         
            var factory = new ConcreteLog();
            for (int i = 0; i < 3; i++)
            {
                if (i == 1)
                {
                    var command = factory.LogCommand("word");
                    command.LogError();
                }
                if (i == 2)
                {
                    var command = factory.LogCommand("csv");
                    command.LogError();
                }

                if (i == 3)
                {
                    var command = factory.LogCommand("sql");
                    command.LogError();
                }
            }
        }
    }
    

Wednesday 10 January 2018

Check for null/empty in dictionary

Dictionary<string, int> queryWhere = new Dictionary<string, int>();
            distDictionary.Add("k1", 1);
            distDictionary.Add("k2", 1000);
            distDictionary.Add("k3", 1600);


private string GetValue(string key)
{
   string returnValue;
   if(!queryWhere.TryGetValue(key, out returnValue))
   {
      returnValue= string.Empty;
   }
   return returnValue;
}

string account = GetValue("kl");
string customer = GetValue("k5");

Sunday 7 January 2018

count odd and even number in C#

 public void EvenOdd()
        {
            int countEven = 0;
            int countOdd = 0;
            int sumOfEven = 0;
            int sumOfOdd = 0;
            Console.WriteLine("insert a number");
            char[] nums = Console.ReadLine().ToCharArray();
            for (int i = 0; i < nums.Length; i++)
            {
                if (int.Parse(nums[i].ToString()) % 2 == 0)
                {
                    countEven++;
                    sumOfEven = sumOfEven + int.Parse(nums[i].ToString());
                }
                else
                {
                    countOdd++;
                    sumOfOdd = sumOfOdd + int.Parse(nums[i].ToString());
                }
            }

            Console.WriteLine($"{countEven} even numbers \n{countOdd} odd numbers \n {sumOfEven} sum of even \n {sumOfOdd} sum of odd");

            Console.WriteLine($"{countEven} \n {countOdd} \n {sumOfEven} \n {sumOfOdd}");
        }

Saturday 6 January 2018

Select and SelectMany: LINQ projection operator

public class Course
    {
        public int Id;
        public string Name;
        public List<string> students;
    }

public static List<Course> Get() {
    List<Course> c1 = new List<Course> {
        new Course {
          Id = 1,
            Name = "Course1",
            students =  new List<string> { "Student1", "Student2", "Student3" }
        },
        new Course {
            Id = 2,
            Name = "Course2",
            students = new List<string> { "StudentA", "StudentB", "StudentC" }
        },
        new Course {
            Id = 3,
           Name = "Course 3",
            students = new List<string> { "StudentX", "StudentY", "StudentZ" }
        }
    };
    return courses;
}

var s = GetCourses().Select(c => c.students);
foreach (var item in s)
{
    Console.WriteLine(item.ToString());
}


Result:

System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]


However, SelectMany

var students = GetCourses().SelectMany(c => c.students);
foreach (var s in students)
{
    Console.WriteLine(s.ToString());
}
Result:

Student1
Student2
Student3
StudentA
StudentB
StudentC
StudentX
StudentY
StudentZ

Friday 5 January 2018

custom filter in angularjs

<HTML ng-app = "myapp">   
    <TITLE> AngularJS: Custom Filter</TITLE>   
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>   
    <script>   
       var myapp=angular.module("myapp",[]);   
       myapp.controller("myappcont",function($scope){   
       $scope.students=[   
       {Name:"Kailash",Gender:"Male",Class:"1Std",Section:"A"},   
       {Name:"Kalyan",Gender:"Male",Class:"1Std",Section:"A"},   
       {Name:"Kalyani",Gender:"Female",Class:"1Std",Section:"A"},   
       {Name:"Kamal",Gender:"Male",Class:"2Std",Section:"B"},   
       {Name:"Keshav",Gender:"Male",Class:"2Std",Section:"B"},   
       {Name:"KedarGouri",Gender:"Female",Class:"2Std",Section:"B"}];   
       });   
 myapp.filter('concatenet',function(){   
 return function(input)   
 {   
 return 'Name:'+input.Name+'Gender:'+input.Gender+'Class:'+input.Class+'Section:'+input.Section;   
 }});               
    </script>   
    <BODY ng-controller="myappcont">   
       <table border="1">   
         <tr>   
            <th>Name</th>   
            <th>Gender</th>   
            <th>Class</th>   
            <th>Section</th>   
                          <th>Concatenet Values</th>                             
         </tr>   
         <tr ng-repeat="student in students | filter:searchText">   
            <td>{{student.Name}}</td>   
            <td>{{student.Gender}}</td>   
            <td>{{student.Class}}</td>   
            <td>{{student.Section}}</td>   
                          <td>{{student|concatenet}}</td>                             
         </tr>   
       </table>   
    </BODY>   
  </HTML>  

delete duplicate row and update reference in child table

--updates the data table to the min ids for each name
update t2
set Id = final_id
from
  t2
join
  t1
on t1.d = t2.Id
join
(
  select
    Name,
    min(Id) as final_id
  from t1
  group by Name
) min_ids
on min_ids.name = t1.name

--deletes redundant ids from the t1 table
delete
from t1
where Id not in
(
  select
    min(id) as final_id
  from t1
  group by name
)