---
title: "AngularJS Expressions and Modules"  
description: "AngularJS Expressions are code snippets usually placed in bindings within the HTML template to bind data to the view."  
author: "Ravi Vishwakarma"  
published: 2024-06-24  
updated: 2024-06-24  
canonical: https://www.mindstick.com/blog/304413/angularjs-expressions-and-modules  
category: "angular js"  
tags: ["web development", "angular js", "front-end development"]  
reading_time: 3 minutes  

---

# AngularJS Expressions and Modules

**AngularJS [Expressions](https://www.mindstick.com/forum/33876/what-is-the-syntax-for-lambda-expressions-in-vb-dot-net)** are code snippets usually placed in bindings within the HTML template to [bind data](https://www.mindstick.com/forum/415/how-to-bind-data-in-windows-phone-7-using-wcf-service) to the view. They can be written inside double [curly braces](https://answers.mindstick.com/qa/31876/when-we-use-curly-braces-in-the-string-format-its-not-working-in-c-sharp) `{{ expression }}` or directly in directives.

**Basic Syntax**: **Interpolation**: `{{ expression }}`

**Example:** `<p>{{ 5 + 5 }}</p>` would display `10`.

**Directives**: **ng-bind**: Another way to bind data.

**Example:** `<p ng-bind="message"></p>`

**Expressions vs [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)**:

- AngularJS expressions are like JavaScript expressions, but they are safe to use in HTML.
- They do not support [control flow](https://www.mindstick.com/interview/33772/what-is-the-control-flow-function-and-its-execution-in-control-flow-statements) statements like `if`, `for`, etc.
- They can include **literals**, **operators**, and **variables**.

**Examples**:

- Mathematical operations: `{{ 1 + 1 }}`
- String [concatenation](https://www.mindstick.com/forum/34153/how-to-write-program-to-file-concatenation-program-in-java-i-o): `{{ "Hello " + name }}`
- Function calls: `{{ myFunction() }}`
- Object access: `{{ user.name }}`

### AngularJS Modules

**AngularJS Modules** are containers for different parts of an [application](https://www.mindstick.com/articles/12824/calculator-application-in-android). They help in separating the application into reusable parts and in [organizing code](https://www.mindstick.com/forum/158791/what-are-rust-s-modules-and-how-are-they-used-for-organizing-code).

**Creating a Module**: Use `angular.module` to create a new module.

**Example:** `var app = angular.module('myApp', []);`

**Adding Dependencies**: You can specify dependencies on other modules.

**Example:** `var app = angular.module('myApp', ['ngRoute', 'ngResource']);`

**Components of a Module**:

- **[Controllers](https://www.mindstick.com/forum/155773/how-asynchronous-controllers-work-in-asp-dot-net-mvc)**: Define the behavior of a particular scope.

```javascript
app.controller('myCtrl', function($scope) {
    $scope.greeting = 'Hello, World!';
});
```

- **Services**: [Share data](https://www.mindstick.com/forum/160500/how-can-you-share-data-between-servlets) and behavior across the application.

```javascript
app.service('myService', function() {
    this.sayHello = function() {
        return 'Hello!';
    };
});
```

- **Directives**: Teach HTML new tricks by extending its functionality.

```javascript
app.directive('myDirective', function() {
    return {
        template: 'This is a custom directive'
    };
});
```

- **Filters**: Format data displayed to the user.

```javascript
app.filter('capitalize', function() {
    return function(input) {
        return input.charAt(0).toUpperCase() + input.slice(1);
    };
});
```

- **Factories**: Similar to services but can return any value.

```javascript
app.factory('myFactory', function() {
    return {
        greet: function() {
            return 'Hello, World!';
        }
    };
});
```

## Example of AngularJS Application

```javascript
<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
    <script>
        // Define a module
        var app = angular.module('myApp', []);

        // Define a controller
        app.controller('myCtrl', function($scope) {
            $scope.message = 'Hello, AngularJS!';
            $scope.updateMessage = function(newMessage) {
                $scope.message = newMessage;
            };
        });

        // Define a service
        app.service('myService', function() {
            this.getGreeting = function() {
                return 'Hello from service!';
            };
        });

        // Define a directive
        app.directive('myDirective', function() {
            return {
                template: '<h1>Custom Directive</h1>'
            };
        });

        // Define a filter
        app.filter('reverse', function() {
            return function(input) {
                return input.split('').reverse().join('');
            };
        });
    </script>
</head>
<body ng-app="myApp">

    <div ng-controller="myCtrl">
        <p>{{ message }}</p>
        <input type="text" ng-model="message">
        <button ng-click="updateMessage('New Message!')">Update Message</button>
    </div>

    <div my-directive></div>

    <p>{{ 'AngularJS' | reverse }}</p>
</body>
</html>
```

---

Original Source: https://www.mindstick.com/blog/304413/angularjs-expressions-and-modules

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
