ng-include is a directive which is used to include HTML contents from other files into the view's HTML template.
For example, if we want to include top, bottom, left and right side bars in the page we need to write it as,
- <div ng-include src="'views/leftsidebar.html'"></div>
- <div ng-include src="'views/rightsidebar.html'"></div>
- <div ng-include src="'views/topsidebar.html'"></div>
- <div ng-include src="'views/bottomsidebar.html'"></div>
What is Isolate Scope and why it is required ?
By default, directives have access to the parent scope in Angular apps. When the parent scope changes at the entire directivethey are no longer useful.
The shared scope allows the parent scope to flow down into the directive but the Isolate scope doesn’t allow the parent scope to flow down into the directive. If you want to make a reusable directive you can’t rely on the parent scope and must use something called Isolate Scope instead. Isolated scope is written as scope: {}
Example of code snippet:
- angular.module('mydirective').directive('sharedScope', function()
- {
- return
- {
- scope: {},
- template: 'Name: {{emp.name}} Address: {{emp.address}}'
- };
- });
How to apply validation in AngularJS?
The angular framework is rich in validation .Below is the code for how to achieve a simple validation.
- <!DOCTYPE html>
- <html>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
-
- <body>
- <h2>
- Validation Example</h2>
- <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate>
- <table style="border: 1px solid gray; width: 500px;">
- <tr>
- <td colspan="2">
- <span style="color: red; padding-left: 100px; border: 3px;">Log In </span>
- </td>
- </tr>
- <br />
- <tr>
- <td>
- Username:
- </td>
- <td>
- <input type="text" name="user" ng-model="user" required>* <span style="color: red" ng-show="myForm.user.$dirty && myForm.user.$invalid"><span ng-show="myForm.user.$error.required">
- Username is required.</span> </span>
- </td>
- </tr>
- <tr>
- <td>
- Email:
- </td>
- <td>
- <input type="email" name="email" ng-model="email" required>* <span style="color: red" ng-show="myForm.email.$dirty && myForm.email.$invalid"><span ng-show="myForm.email.$error.required">
- Email is required.</span> <span ng-show="myForm.email.$error.email">Invalid email address.</span>
- </span>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <p>
- <input type="submit" ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
- myForm.email.$dirty && myForm.email.$invalid">
- </p>
- </td>
- </tr>
- </table>
- </form>
- <script>
- var app = angular.module('myApp', []);
- app.controller('validateCtrl', function($scope) {
- $scope.user = 'Pradeep K';
- $scope.email = '[email protected]';
- });
- </script>
- </body>
-
- </html>
How to do custom form validation in AngularJS?
To achieve the custom form validation we need to write directives. For Example we want to show the number of characters left for a text area when the user enters text to a text area.
Code Example:
- <!DOCTYPE html>
- <html ng-app="myapp" xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
- <script>
- var $scope;
- var app = angular.module('myapp', []);
- app.controller('Ctrl', function($scope)
- {
- $scope.wks =
- {
- number: 1,
- validity: true
- }
- });
- app.directive('isNumber', function()
-
- {
- return
- {
- require: 'ngModel',
- link: function(scope)
- {
- scope.$watch('wks.number', function(newValue, oldValue)
- {
- var arr = String(newValue).split("");
- if (arr.length === 0) return;
- if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.')) return;
- if (arr.length === 2 && newValue === '-.') return;
- if (isNaN(newValue))
- {
- scope.wks.number = oldValue;
- }
- });
- }
- };
- });
- </script>
- </head>
-
- <body ng-app="myapp">
- <form ng-app="myapp" name="myform" novalidate>
- <div ng-controller="Ctrl"></div>
- <textarea ng-model="test" ng-trim="false" maxlength="100"></textarea>
- <span>{{100 - test.length}} left</span>
- </form>
- </body>
-
- </html> .
What is a custom filter, write a own custom filter
Custom Filters are used to extend the behavior of binding expressions based on our own logic.
Example:
- angular.module("testModule").filter("currencyFilter", function()
- {
- return function(val)
- {
- return "Rs. " + val * 67;
- };
- });
- In the HTML use it as:
- <
- span >
- {
- {
- price | currencyFilter
- }
- } < /span>
How to do DOM manipulation inside a directive?
DOM manipulations are done in custom directive.
Here is a Code example,
- app.directive('myDirective', function()
- {
- return
- {
- restrict: 'A',
- templateUrl: 'directives/template.html',
- link: function(scope, element, attr)
- {
- $(element).css('font-weight', 'bold');
- }
- };
- });
How to communicate between the controllers in AngularUI Pages ?
Angular provides the $broadcast, $on and $emit services for event-based communication among controllers. This is very similar to the publisher subscriber pattern.
We can use root scope or service to pass data across controllers:
- $broadcast: dispatches the event downwards to all child scopes.
- $emit: dispatches the event upwards using the scope hierarchy.
- $on: listen on the events of a given type. It can catch the event dispatched by $broadcast and $emit.
What are the Angular 2.0 new features in compared to angular previous versions?
There are drastic changes in 2.0 in compared to earlier 1.x versions .Some of the new features are
- Component Based routing
- No Scope use
- Directive based manipulation
- More test support
Can you explain Angular 2.0 routing and how it is defined?
Angular 2.0 comes up with folder based structures as component.ng-viewport is the place holder where we will load the content of the page.
Let us consider we have different Tabs, such as news, stock, whether, stocks. On click of each tab we will load different views.
This is the code we need to follow.
- <body ng-controller="ComponentController">
- <h1>
- Component Wise Routing in Angular Js</h1>
- <a ng-link="news()" class="tab">News</a>
- <a ng-link="stock()" class="tab">Stock</a>
- <a ng-link="weather()" class="tab">Weather</a>
- <a ng-link="sports()" class="tab">Sports</a>
- <div ng-viewport>
- </div>
- <script>
- var module = angular.module("myApp", ["ngNewRouter", "myApp.news", "myApp.stock", "myApp.weather", "myApp.sports"]);
- module.controller("ComponentController", function($scope, $router) {
- $router.config(
- [{
- path: "/",
- redirectTo: "news"
- },
- {
- path: "/stock",
- component: "stock"
- },
- {
- path: "/weather",
- component: "weather"
- },
- {
- path: "/sports",
- component: "sports"
- }]);
- });
- </script>
- <script src="components/news/news.js"></script>
- <script src="components/stock/stock.js"></script>
- <script src="components/weather/weather.js"></script>
- <script src="components/sports/sports.js"></script>
- </body>
ng-viewport
In order to inject an HTML template known as component into a web page, we need to create a container/placeholder div for it. This div should have an Angular directive ng-viewport. This ng-viewport is like ng-view/ui-view from Angular UI router and AngularJS core functionality. The directive ng-viewport can be used any number of times inside an application.
Component
This is an Angular service which allows us to retrieve the current set of route parameters. Componet looks for components folder inside the project structure.
In the Angular code how to avoid the error “is not allowed by Access-control-Allow-Origin”?
To enable CORS in Web API, use the Microsoft.AspNet.WebApi.Cors package, which is available on NuGet.
config.Enablecors() to the register() in the WebAPIconfig.cs
How to bind a data to the ng-grid ?
Showing the Data in grid and editing and deleting records in a grid is a common requirement in a project.
- var app = angular.module('myApp', ['ngGrid']);
- app.controller('MyCtrl', function($scope)
- {
- $scope.myData = [{
- name: "Moroni",
- age: 50
- }, {
- name: "Tiancum",
- age: 43
- }, {
- name: "Jacob",
- age: 27
- }, {
- name: "Nephi",
- age: 29
- }, {
- name: "Enos",
- age: 34
- }];
- $scope.gridOptions =
- {
- data: 'myData',
- columnDefs: [
- {
- field: 'name',
- displayName: 'Name'
- },
- {
- field: 'age',
- displayName: 'Age'
- }]
- };
- }); <
- body ng - controller = "MyCtrl" >
- <
- div class = "gridStyle"
- ng - grid = "gridOptions" > < /div> <
- /body>
These are some of the concepts and real time questions. Thanks for reading!
Read more articles on AngularJS: