Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

$rootScope in Angular JS

Posted in Angular JS

rootscope
$rootScope refers to an object which is accessible from everywhere of the application. You can think $rootScope as global variable which can be accessible in all controllers, services, directives. The $rootScope object has a function called $new() that’s used to create child scopes.

Angular JS finds ng-app and therefore creates a $rootScope object. Next, while traversing the DOM, Angular JS encounters another ng-controller directive which points to “myController”. Now, it creates another child scope using $rootScope.$new() called $scope.

Injecting rootScope in a controller:
rootscope

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Multiple ng-class in angular JS

Posted in Angular JS

The ng-class directive is used every time you need to dynamically apply a class to an element by providing the name of the class in a data-binding expression. We can add multiple classes based on the multiple conditions in angular JS.

Here is the example demonstrating adding multiple ng-class directive:

<div ng-class="{'class1': expression1,'class2':expression2}"> ... </div>

Add class using ternary operator:

   <div ng-class="expression?'className1':'className2'"> ... </div>

For example, if you want to add active class to your menu when menu button is clicked & toggle class when menu button is clicked again then refer example below.

   <div ng-class="menuActive?'active':''"> ... </div>

In above case, menuActive is a scope variable & it is toggled when menu button is clicked. When menuActive is clicked active class is added to the menu & when it is clicked again active class is removed. It is very similar to JQuery addClass & removeClass methods.

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

ng-options in Angular JS

Posted in Angular JS

ng-

Angular JS provides ng-options directive to bind HTML to select dropdown with options. With ng-options use ng-model to store selected options in dropdown. Here is an example. Angular controller has collection of authors scope variable.

var myApp = angular.module('myApp');
myApp.controller('myCtrl', ['$scope', function($scope){
      $scope.authors=[
                {
                    id:0,
                    name:"John Daniel",
                    age:"34",
                    books:[
                        {
                                 name:"Fundamentals of Chemistry",
                                 publishedDate:"07-23-2014"
                        },{
                                 name:"Train Your Brain",
                                 publishedDate:"7-03-2016"
                        },
                        {
                                name:"Business Economics",
                                publishedDate:"12-12-2015"
                        }
                    ]
                
                },{
                        id:1,
                        name:"David Manthon",
                        age:"57",
                        books:[
                            {
                                name:"Principles of Algebra",
                                publishedDate:"11-01-2013"
                            },{
                                name:"Think and Grow",
                                publishedDate:"09-12-2015"
                            },{
                                name:"Finance and Marketing",
                                publishedDate:"11-13-2012"
                            },
                            {
                                name:"The Art Of War",
                                publishedDate:"06-05-2011"
                            }
                    ]

                },{
                        id:2,
                        name:"Melisa Bosch",
                        age:"39",
                        books:[
                            {
                                name:"Java for Beginners",
                                publishedDate:"10-10-2012"
                            },
                            {
                                name:"Stay Hungry Stay Foolish",
                                publishedDate:"08-11-2012"
                            }
                      ]
                },
                {
                        id:3,
                        name:"Simeck Volja",
                        age:"42",
                        books:[
                            {
                                name:"Pro C Programming",
                                publishedDate:"01-02-2015"
                            },
                            {
                                name:"You Can Win",
                                publishedDate:"11-20-2013"
                            }
                    ]
                }
          ]
}]);

HTML Template:

<body ng-app="myApp">
    <div ng-controller="myCtrl">
<h2>Authors</h2>
  <ul class="list-group">
    <li class="list-group-item" ng-repeat="author in authors" > {{author.name}}
      <br/>
      <strong>Books:</strong>
      <select class="form-control" ng-model="author.selectedbook" ng-options="book.name for book in author.books" >
        <option value="">--Select--</option>
      </select>
      <strong>Selected Book:</strong>{{author.selectedbook.name}}
    </li>
    
  </ul>

   </div>
</body>

We will use ng-repeat directive to iterate all objects in authors’s collection & then loop through each book object in books array of each author.

Use list-group bootstrap class to the li tag to display a visually formatted list. Here, we are iterating through each author object & displaying each author name using {{author.name}} angular expression tag. Each book in books array is bind to every option of select dropdown.

ng-model: we have used author.selectedbook as ng-model here to store selected book. By doing this there is one more property called selectedbook created in each author object and the same is available in controller’s scope.

When we select any book of particular author the value of select dropdown is bind to the author.selectedbook & we can access name property of each book like this author.selectedbook.name.

Refer fig. below.

angular-ng-options

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Get URL parameters using JavaScript

Posted in Javascript, javascript interview questions

Sometimes you might need to get URL parameters also called as query string parameters using JavaScript. URL parameters can be one or multiple.

Example:

http://localhost/mypage?id=10

Multiple URL parameters:

http://localhost/mypage?id=10&name=Simy

Use script below to get URL parameters:


function getURLParameter(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Above function returns specified URL parameter based on the argument supplied to that function.

getURLParameter(“id”) –> Returns 10

getURLParameter(“name”) –> Returns Simy

Web development blog for Html, CSS, javascript, JQuery developers. At Kodecrash you will find best solution for complex problems.

Custom select dropdown using CSS

Posted in CSS

We can create customize default HTML select dropdown using CSS. We can either customize this using JS. Refer Create Custom Dropdown using jQuery article to create custom dropdown using JavaScript.

Use CSS below:

<style>
	
  select.custom-dropdown {
    -webkit-appearance: none;
    -moz-appearance: none;
    /* border: 0 !important; */
    /* background-color: #0d98e8; */
    color: #333;
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    font-size: 14px;
    padding: 10px;
    width: 245px;
    cursor: pointer;
    background: url(drop-down-arrow.png) no-repeat right center;
    background-size: 40px 37px;
    border: 1px solid #CCC;
    background-position: 206px 0;

 }
  select::-ms-expand {
    display: none;
 }
</style>

Use above CSS to customize select dropdown. This CSS is compatible with all browsers including IE 10+, FF, Chrome and MAC Safari.

Download Code

Demo: