Skip to content
AngularJS

Module & Controller

Define a module and a controller with scope methods.

#angularjs#module#controller

Code

angularjs
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>