CamilleSNCF
CamilleSNCF

Reputation: 39

AngularJS ng-controller undefined

I'm working on a small angular project, in which I make use of the bootstrap calendar by mattlewis92, and I keep having problem with my controller.

Here is my .js file :

'use strict';

var app = angular.module('Calendrier574', ['ngMaterial', 'ngRoute', 'mwl.calendar', 'ui.bootstrap', 'ngTouch', 'ngAnimate']);

app.controller("cal574", function(moment, alert, $timeout, $log) {
var vm = this;

vm.events = [];
vm.calendarView = 'day';
vm.viewDate = moment().startOf('month').toDate();

vm.isCellOpen = true;

vm.toggle = function($event, field, event) {
  $event.preventDefault();
  $event.stopPropagation();
  event[field] = !event[field];
};

});
app.config(function(calendarConfig) {
  calendarConfig.dateFormatter = 'moment';
});

I want to be able to use two controllers, since I would like two versions of the same calendar on the same page, one showing a day view, the other a month view.

So I have this in my .html file

<html lang="fr" ng-app="Calendrier574">

And this on one of my div :

<md-content id="content" ng-controller="cal574 as vm" layout-padding flex>

But I keep getting the "Argument 'cal574' is not a function, got undefined". I went through the posts that have already been done on the subject but I couldn't find anything helpful.

If you need anything else please tell me.

I'm using angular 1.5.5 by the way.

EDIT : I created a jsfiddle, even so there are missing dependencies it might be of some help to have a better look at the code https://jsfiddle.net/zzddpk4v/#&togetherjs=s8M8Vir3rc

DOUBLE EDIT : Still looking for a solution, I'm working on cloud9 so if anyone wants to look at the whole code almost working, and try to edit it directly, you can go check it here https://ide.c9.io/millenium/back574upsidenav-cloned

Upvotes: 3

Views: 612

Answers (3)

rrd
rrd

Reputation: 5957

Trying changing this line:

app.controller("cal574", function(moment, alert, $timeout, $log) {

To:

app.controller("cal574", ['moment', 'alert', '$timeout', '$log', function(moment, alert, $timeout, $log) {

Then add in the corresponding extra ] at the end of the controller.

Upvotes: 0

Swati Maheshwari
Swati Maheshwari

Reputation: 85

Have you included all the related .js files in your project/html.

  <script src="../path to controller/controller.js"></script>

I got the same error when i forgot to include .js file.

Upvotes: 1

Tom Shen
Tom Shen

Reputation: 1045

You need to wrap your controller in a function :

(function() {
    'use strict';
    // your controller code here
})();

Upvotes: 0

Related Questions