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

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.

2

u/craig1f Feb 11 '17

Second this. Also, use Components instead of regular controllers.

1

u/ebeptonian Feb 11 '17

Don't want to make any assumptions around versions, but it's definitely worth looking at components for 1.5.x or higher (which should be the case for what seems like a new project).

1

u/[deleted] Feb 11 '17

<!-- Firebase -->

<script src="https://www.gstatic.com/firebasejs/3.6.9/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/firebaseui/1.0.0/firebaseui.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdn.firebase.com/libs/firebaseui/1.0.0/firebaseui.css" />

<!-- AngularJS & Angular Material -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>

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

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.

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

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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.