Directives – Directives are attributes decorated on HTML elements. All directives start with the word “ng”. AngularJS has a set of built in directives which offers functionality to the application. You can also define your own directives in AngularJS
- ng-app – This directive initializes and AngularJS application.
- ng-bind – This directive binds the content of an HTML element to application data.
- ng-class - This directive specifies CSS classes on HTML elements.
- ng-checked – This directive specifies if an element is checked or not.
- ng-click – This directive specifies and expression to evaluate when and element is being clicked.
- ng-controller – This directive defines the controller object for an application.
- ng-init – This directive initializes application data.
- ng-model – This directive binds the value of HTML controls to application data.
- ng-selected – This directive specifies the selected attribute of an element.
- ng-repeat – This directive defines a template for each data in a collection.
- ng-required – This directive specifies the required attribute of an element.
- ng-show – This directive defines show and hide HTML elements.
- ng-submit – This directive specifies the expressions to run on onsubmit event
- ng-value – This directive specifies the value of an input element.
ng-app
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <script src="Scripts/angular.min.js"></script>
- <script src="Scripts/angular-route.min.js"></script>
- <script src="app.js"></script>
- <script src="controller.js"></script>
- </head>
- <body>
- <div ng-app="app" ng-controller="controller">
- <h1> {{ title }} {{ date }}</h1>
- </div>
- </body>
- </html>
Other way is you can write app and controller code inside script tag on same page like this.
- <div ng-app="app" ng-controller="controller">
- <h1> {{ title }} {{ date }}</h1>
- </div>
- <script>
- var app = angular.module("app", []);
- app.controller("controller", function ($scope) {
- $scope.title = "This is test message by RB! on ";
- $scope.date = new Date();
- });
- </script>
- <div ng-app="app" ng-init="title='This is test message by RB!'">
- <p ng-bind="title"></p>
- </div>
OR
- <div ng-app="app" ng-controller="controller">
- <label>Title: <input type="text" ng-model="title"></label><br>
- <h1 ng-bind="title"></h1><h3 ng-bind="date"></h3>
- </div>
- <script>
- var app = angular.module("app", []);
- app.controller("controller", function ($scope) {
- $scope.title = "This is test message by RB! on ";
- $scope.date = new Date();
- });
- </script>

ng-class
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <script src="Scripts/angular.min.js"></script>
- <style>
- .CSharp {
- color: white;
- background-color: lightblue;
- padding: 20px;
- font-family: "Courier New";
- }
- .ASPNET {
- background-color: coral;
- padding: 40px;
- font-family: Verdana;
- }
- .VBNET {
- background-color: brown;
- padding: 40px;
- font-family: Verdana;
- }
- .red {
- color: red;
- }
- .green {
- color: green;
- }
- .blue {
- color: blue;
- }
- </style>
- </head>
- <body ng-app="">
- <p>Choose a technology:</p>
- <select ng-model="home">
- <option value="CSharp">C#</option>
- <option value="ASPNET">ASP.NET</option>
- <option value="VBNET">VB.NET</option>
- </select>
- <div ng-class="home">
- <h1>This is test message by RB!</h1>
- </div>
- <div>
- <button ng-click="areaStatus = !areaStatus">Toggle</button>
- <div ng-class="{'red' : areaStatus, 'green' : !areaStatus}">
- This is some text
- </div>
- </div>
- </body>
- </html>

ng-checked
- <div ng-app="app" ng-controller="controller">
- Check Me: <input type="checkbox" ng-model="checked">
- This Will Check Automatically: <input id="checkTest" type="checkbox" ng-checked="checked">
- </div>
- <br />
- <script>
- var app = angular.module("app", []);
- app.controller("controller", function ($scope) {
- $scope.checked = false;
- });
- </script>
- <div ng-app="">
- <p>Fav Technologies:</p>
- <input type="checkbox" ng-model="all"> Check all<br><br>
- <input type="checkbox" ng-checked="all">C#<br>
- <input type="checkbox" ng-checked="all">ASP.NET<br>
- <input type="checkbox" ng-checked="all">VB.NET<br />
- <input type="checkbox" ng-checked="all">MVC<br />
- <input type="checkbox" ng-checked="all">AngularJS
- </div>

In above screen when you click on check all check box all check boxes will be checked.
ng-click
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <script src="Scripts/angular.min.js"></script>
- </head>
- <body>
- <div ng-app="app" ng-controller="controller">
- <button ng-click="myFunc()">OK</button>
- <p>The button has been clicked {{count}} times.</p>
- <button ng-click="submit()">Submit</button>
- <p>Alert - {{msg}}</p>
- </div>
- <script>
- var app = angular.module("app", []);
- app.controller("controller", function ($scope) {
- $scope.count = 0;
- $scope.myFunc = function () {
- $scope.count++;
- };
- $scope.submit = function () {
- $scope.msg = "Congrats! you clicked a submit button.";
- };
- });
- </script>
- </body>
- </html>

In given code, we have two buttons and two functions. One button invokes a function which increase a number and other function display a message when click on submit button.
ng-controller
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <script src="Scripts/angular.min.js"></script>
- </head>
- <body>
- <div ng-app="app" ng-controller="controller">
- <h2>Hello! My Name is {{firstname}} {{lastname}}</h2>
- </div>
- <script>
- var app = angular.module("app", []);
- app.controller("controller", function ($scope) {
- $scope.firstname = "Raj";
- $scope.lastname = "Kumar";
- });
- </script>
- </body>
- </html>

ng-init
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <script src="Scripts/angular.min.js"></script>
- </head>
- <body>
- <div ng-app="app" ng-init="title='This is C# Contribution of Raj Kumar!';TotalArticles=288;TotalBlogs=49">
- Article: <input type="text" ng-model="TotalArticles" />
- Blogs: <input type="text" ng-model="TotalBlogs" />
- Total Contribution: {{TotalArticles + TotalBlogs}}
- <div>
- <p style="font-size:24px;" ng-bind="title"></p>
- </div>
- </div>
- <script>
- var app = angular.module("app", []);
- </script>
- </body>
- </html>








Satish Kumar VadlavalliPosted Dec 23, 2016, 1:55 AM
Nice share, keep up good work