Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS controller function never called

Tags:

My angularjs controller function is never called. I see all the js files included in my sources, it's just not ever hitting the initialization of my controller. What is wrong with this code?

eventListCtrl.js:

eventsApp.controller('eventListCtrl', ['$scope', function ($scope) {     // this code is never called     alert('controller initializing');     $scope.name = "Test"; }]); 

app.js:

'use strict';  var eventsApp = angular.module('eventsApp', []); 

Index.cshtml:

<div ng-controller="eventListCtrl">     <div class="row">         <div class="spann11">             <h2>{{name}}</h2>         </div>     </div> </div>  <script src="/scripts/vendor/angular.js"></script> <script src="/scripts/app.js"></script> <script src="/scripts/controllers/eventListCtrl.js"></script> 
like image 716
Ryan Langton Avatar asked Mar 24 '14 15:03

Ryan Langton


2 Answers

You need to do this

<html ng-app="eventsApp"> 

So you actually activate the module

like image 57
iConnor Avatar answered Sep 18 '22 23:09

iConnor


I want to post my issue/resolution here in case anyone does the same thing as me.

<div ng-controller="coolCtrl" ng-if="VAR"> 

I was trying to init a controller on an element that didn't get created (because of the ng-if="var" )
facepalm

like image 31
James Harrington Avatar answered Sep 18 '22 23:09

James Harrington