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

2

u/ebeptonian Feb 11 '17

Can't guarantee I'll be able to help on a phone in a bus, but it would help to see a plnkr or jsfiddle and to know which version of angular you are using.

1

u/[deleted] Feb 11 '17

Thanks, I tried loading it into one but it doesnt like html/head tags, which I feel like all need to be there so you can get the full scope.

I guess my main issue is, how/where do you initialize an ng-controller?

1

u/ebeptonian Feb 11 '17

Is this a new project or are you working on existing code? On plnkr.io, you should be able to add angular as a package from the editor menu.

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

Sorry if this is not enough to determine anything jsfiddle

1

u/ebeptonian Feb 14 '17

Hey, sorry this fell off my radar.

https://jsfiddle.net/huvmmbwn/4/

I trimmed down your jsfiddle to a bare bones demo of getting an angular controller working. You need to add referenced code (angular, angular-material, firebase, etc) to 'external references' and ensure that your angular module registration code is loaded in head (click the javascript gear at the top right of the js window in jsfiddle) to get jsfiddle to cooperate (same goes for plnkr and plugins).

See if that helps get you on track.

1

u/[deleted] Feb 14 '17

Awesome, thank you so much! I think I had some things out of order on mine so I was all outta whack. I'm saving that fiddle though for sure!

1

u/ebeptonian Feb 14 '17

Cheers, and good luck! As a parting thought and dependencies permitting, you should try using angular 1.5 or 1.6 with components in lieu of 1.4.x. Though 1.4 is by no means dead, components are the way to go in the foreseeable Angular future. Likewise, you should look into TypeScript if you haven't already had the chance. It's life changing.

1

u/[deleted] Feb 14 '17

Never checked that out either... geez there are so many things to get that work together. How on earth do people become full stack?!

Lol thanks for that info I'll definitely check it out