Wai Yan Hein Wai Yan Hein - 2 months ago 16
AngularJS Question

Cannot declare controller in Angular Js

I am developing an Web Application. In my application, I am using Angular JS. Frankly, I am new to Angular JS. But now I am having a problem my declaring controller. Please see my code below.

<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. So please what is wrong with my code? Please why my controller not working? Please help me.

Answer

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){

}]);
Comments