0

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?

5
  • 1
    I don't see your controller defined anywhere, only you calling it when it doesn't exist Commented Sep 30, 2016 at 14:24
  • 1
    You declared a controller that you didn't define. Remove it from your DOM or declare it as following : app.controller('DefaultController', function(){}); Commented Sep 30, 2016 at 14:24
  • As you can see I added ng-controller="DefaultController" besides ng-app declaration. @SterlingArcher Commented Sep 30, 2016 at 14:27
  • 1
    There's a major difference between adding the controller, and defining it. ng-controller expects the controller to exist, and you haven't defined one. Commented Sep 30, 2016 at 14:28
  • Here is a couple of mini-advices: 1.You are missing the open '<body> tag 2. You should put the script bloque just before of closing the '</body>' tag. 3. Read the answer from @Mickers Commented Sep 30, 2016 at 14:37

2 Answers 2

2

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

}]);
Sign up to request clarification or add additional context in comments.

Comments

2

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']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.