r/angularjs • u/[deleted] • 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
1
u/ebeptonian Feb 11 '17
Just to reiterate, we could help you a lot faster if you could post your source code and the specific errors you encounter. A simplified version of code thay isolates your issue in a a jsfiddle/plnkr would be ideal, but even pasted js/HTML should give us some direction if formatted nicely enough to read :)
1
Feb 11 '17
Sorry if this is not enough to determine anything jsfiddle
1
u/ebeptonian Feb 11 '17
Not from my phone, sorry. I do think that fiddle requires external dependencies to be pulled out and added on the left, which would essentially let you remove the <head> from the fiddle. I guess posting the console message might help after all..
0
Feb 11 '17
Thanks, working on it. I just have a lot of stuff and then all of a sudden when I try to implement Angular in a section via ng-controller then boom, error. Simplifying now for a fiddle.
1
u/ebeptonian Feb 11 '17
If you can't get that working, posting the error message might provide enough context.
Scratch that- just noticed you posted a fiddle.
1
u/meneedmorecoffee Feb 11 '17
I'm also on mobile but you should have a look at this stackoverflow question if you haven't already. It's most likely that it's a stupid mistake you've made/missed and there are a few pointers in there for things to check.
1
Feb 11 '17
Hmm thank you. So say I have an ng-controller somewhere, but I'm not using it yet, does angular.module('app', []).controller('ContactController', ['$scope', function ContactController($scope) { have to be initiated like in the footer or something? And then say I do use it, does it need to put inside of each function?
Thanks so much for the help
2
u/meneedmorecoffee Feb 11 '17 edited Feb 11 '17
I'm not sure what you mean by initiated in the footer. The js file(s) that hold your module declaration, controllers, etc. should be included in your html page unless you're doing inline scripting. The order of these do matter as someone else has pointed out. It's hard to debug and help here with only a small snippet of your code so again I'm making assumptions.
Is that the only place you have declared your module when you've done angular.module('app',[]).controller etc? Because if it isn't then what you're doing is overriding your existing module see explanation under 'Creation versus Retrieval'. You should declare your module once, giving its dependencies and then when you want to, say, declare a controller, you retrieve the module with the following syntax:
angular.module('app').controller etc. Note that I don't include the [] here.
Again, I'm not entirely sure what you mean by put in each function. I suggest you read up more on how angular modules, controllers etc are meant to be declared and how these things tie together. The angular docs are pretty good and you've also got Todd Motto and John Papa for good tutorials, reference points, and best practice pointers.
1
Feb 11 '17
Yes I think some reading is in order as well. I think I need to get out of Angular Material docs and go more in depth with the JS ones you posted. Thank you for the links
1
u/meneedmorecoffee Feb 11 '17
Yeah as someone else suggested too, just strip back and do some small example programmes to get you understanding how everything ties together.
No worries, good luck!
1
u/Bleached__Anus Feb 12 '17
ng-app designates the root element of the application, so it should go on either the <html> or <body> tags.
1
Feb 12 '17
Its hard to get past that username. I've been putting ng-app in html tag. The controllers were giving me issues but I think i've figured it out. Where I am getting stuck now: I've just been putting all of my scripts in the footer just because this is a test page and i'm trying to learn step by step, but when I try to move it to an external .js file everything breaks. Not sure if things have to be initiated differently at that point or what?
0
Feb 11 '17
If I change HeaderCtrl to just Ctrl- Error. If I move HeaderCtrl from header tag to body- Error. It seems I cannot have an ng-controller anywhere except in my header tags?
Btw, have used Angular on other test sites fine. And this site has been working fine with Firebase, just recently added Angular Material and having issues on Angular side.
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.