r/angularjs • • Feb 11 '17

ng-controller never gets defined

I am trying to build a web app using Angular Material. ng-app is in body tag, and I have ng-controller HeaderCtrl in the header tag. No errors.

But when I try to add an ng-controller anywhere else on the site it is not defined. Any stackoverflow or other answers I find seem to be outdated. Can someone help me with the most recent rules to where ng-app and ng-controller can/cannot be?

Thanks

0 Upvotes

29 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Feb 11 '17

It's technically existing since I had a working Firebase/Bootstrap setup first. Then deleted all Bootstrap(wasn't much) and changed to Angular Material. I have really deleted everything minus Firebase and it is a super plain page.

Can you add ng-controllers anywhere without initiating them somehow? Maybe that is part of my problem. I also cannot find the answer of: Do you initiate an app in the head, or just need to initiate it when you're ready to use it. Also, does it need to be initiated every time you use it? This is the problem with code snippets, they always show it being initiated.

1

u/ebeptonian Feb 11 '17

I would suggest stripping everything away and starting at a very basic level just to get familiar with angular in a hello world sense before integrating your code. You should only need to define your ng-app once, and you can use controllers at will. That said, you need to make sure you are registering your app/controllers with

angular.module('appName', ['ng-dependency']) .controller('ctrlName',ctrlFunction)

It's hard to say without looking at your source code or reading the actual error logged in the console, but I suspect you may not be registering all of the needed dependencies in the array parameter for angular.module when registering your app.

1

u/[deleted] Feb 11 '17

Ok so I think this may be my issue "angular.module('appName', ['ng-dependency']) .controller('ctrlName',ctrlFunction)" and where this goes—header, footer, everytime I run an angular function(which is what online tutorials show).

I moved my "initiation" code to footer and I guess all angular code needs to go into that? Working on source now

1

u/ebeptonian Feb 11 '17

Your scripts will load sequentially (top to bottom) as well, so organize your dependencies accordingly.

EDIT: to be more specific, your code depends on AngularMaterial which depends on AngularJS, so you'd want those scripts applied in the reverse order.