What is Computer

Module: M1-R5: Information Technology Tools and Network Basics

Chapter: Ch1 Computer Intro

🔹 Introduction to AngularJS Directives

Directives in AngularJS are markers on DOM elements (attributes, elements, comments) that tell AngularJS's HTML compiler ($compile) to attach a specified behavior. They extend HTML functionality. Examples are ng-app, ng-model, ng-repeat, etc.

1. ng-app Directive

<!DOCTYPE html>
<html ng-app="myApp">
<head><title>ng-app Example</title></head>
<body>
  <h1>AngularJS App Example</h1>
</body>
</html>

2. ng-model Directive

<div ng-app="myApp">
  <input type="text" ng-model="name">
  <p>Hello, {{ name }}!</p>
</div>

3. ng-bind Directive

<div ng-app="myApp" ng-controller="myCtrl">
  <p ng-bind="message"></p>
</div>

<script>
  var app = angular.module("myApp", []);
  app.controller("myCtrl", function($scope){
    $scope.message = "Hello from ng-bind!";
  });
</script>

4. ng-repeat Directive

<div ng-app="myApp">
  <ul>
    <li ng-repeat="item in ['Apple','Banana','Orange']">{{ item }}</li>
  </ul>
</div>

5. ng-click Directive

<div ng-app="myApp">
  <button ng-click="count = count + 1">Click Me</button>
  <p>Clicked: {{ count }} times</p>
</div>

6. ng-show / ng-hide Directive

<div ng-app="myApp">
  <button ng-click="visible = !visible">Toggle</button>
  <p ng-show="visible">This text is visible!</p>
</div>

7. ng-if Directive

<div ng-app="myApp">
  <input type="checkbox" ng-model="checked"> Show message
  <p ng-if="checked">Checkbox is checked!</p>
</div>

8. ng-init Directive

<div ng-app="myApp">
  <p ng-init="greeting='Hello AngularJS'">{{ greeting }}</p>
</div>

9. ng-class Directive

<div ng-app="myApp">
  <button ng-click="red = !red">Toggle Red</button>
  <p ng-class="{redClass:red}">This text changes color</p>
</div>

<style>
.redClass { color: red; font-weight: bold; }
</style>

10. ng-style Directive

<div ng-app="myApp">
  <p ng-style="{color: myColor, 'font-size': '20px'}">Styled text</p>
</div>

<script>
  var app = angular.module("myApp", []);
  app.controller("ctrl", function($scope){
    $scope.myColor = 'blue';
  });
</script>
Quick Links