Ryan Langton
Ryan Langton

Reputation: 6160

AngularJS controller function never called

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>

Upvotes: 22

Views: 22765

Answers (4)

Arvind CR
Arvind CR

Reputation: 11

<html ng-app="eventsApp">
<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>
</html>

This should fix it

Upvotes: 0

James Harrington
James Harrington

Reputation: 3216

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

Upvotes: 2

iConnor
iConnor

Reputation: 20199

You need to do this

<html ng-app="eventsApp">

So you actually activate the module

Upvotes: 45

Joe Minichino
Joe Minichino

Reputation: 2773

Wild guess:

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

in your controller file?

Upvotes: 0

Related Questions