Skip to content
","url":"https://ez4code.com/snippets/angularjs-module-controller","keywords":"angularjs, module, controller","author":{"@type":"Person","name":"EZ4Code Team"},"publisher":{"@type":"Organization","name":"EZ4Code","logo":{"@type":"ImageObject","url":"https://ez4code.com/logo.png"}},"datePublished":"2024-01-01","dateModified":"2026-08-01","image":"https://ez4code.com/og-image.png"}
AngularJS

Module & Controller

Define a module and a controller with scope methods.

By EZ4Code Team
angularjsmodulecontroller

Code

angular.module('myApp', []);

angular.module('myApp').controller('MainController', function($scope) {
  $scope.title = 'Hello AngularJS';
  $scope.count = 0;

  $scope.increment = function() {
    $scope.count++;
  };

  $scope.reset = function() {
    $scope.count = 0;
  };
});

<!-- HTML usage -->
<body ng-app="myApp" ng-controller="MainController">
  <h1>{{ title }}</h1>
  <button ng-click="increment()">+1</button>
  <button ng-click="reset()">Reset</button>
  <p>Count: {{ count }}</p>
</body>

Explanation

angular.module('myApp', []) declares a module (the array lists dependencies); retrieving it without the array getter retrieves an existing module. Controllers attach state and behavior to $scope, which the template binds to via {{ }} interpolation and ng-click. ng-app bootstraps the module on the element.

More AngularJS Snippets