Wai Yan Hein
Wai Yan Hein

Reputation: 14831

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?

Upvotes: 0

Views: 83

Answers (2)

Manikanta Prasanth
Manikanta Prasanth

Reputation: 71

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

}]);

Upvotes: 2

Mickers
Mickers

Reputation: 1449

You need to define your controller somewhere and add it as a dependency to the app first. Example:

angular.module('app.controllers', [])
    .controller('DefaultController' function() {
        //do stuff
    };

and in your app definition:

var app = angular.module('memeApp',['ngRoute','ui.bootstrap', 'app.controllers']);

Upvotes: 2

Related Questions