Search code examples
angularjsangularjs-directiveangularjs-controller

Cannot declare controller in Angular Js


I am developing a web application. In my application, I am using Angular JS. I am new to Angular JS. But now I am having a problem my declaring controller. Here is my code.

<html>
<head>
 //files references
</head>
<script>
var app = angular.module('memeApp',['ngRoute','ui.bootstrap']);
</script>
<nav class="nav-bar">
<a href="page1">Home</a>
<a href="page2">Account</a>
</nav>
<div class="content" ng-app="memeApp" ng-controller="DefaultController">
//content
</div>
</div>
</body>
</html>

As you can see I did nothing yet. I declare a controller named DefaultController. So when I check log, it is giving me following error:

enter image description here

So my controller is totally not working. When I add js code for controller as well. If I removed controller directive, errors gone. Why is my controller not working?


Solution

  • You need to define a controller function 'DefaultController' before you use it in html div tag.

    Add below code in your script tag.

    app.controller('DefaultController', ['$scope', function($scope){
    
    }]);