0

My codes looks like this

angular
.module('main', ['ngRoute'])
.config(['$routeProvider',
    ($routeProvider) ->
    $routeProvider
    .when '/',
      templateUrl: 'homePage/homePage.html'
      controller: 'MainCtrl'
])

angular.module('main').controller('MainCtrl',
 ['$scope' , ($scope) ->
    $scope.test = {}])

The browser will compain Error: [ng:areq] Argument 'MainCtrl' is not a function, got Object.

But if I don't use the inline array dependency injection in MainCtrl and rewrite it like this:

angular.module('main').controller('MainCtrl',
 ($scope) ->
    $scope.test = {})

Then everything works well. Does anyone have ideas about this? Thanks!

0

1 Answer 1

2

As the error message is very clear on the fact that issue is not with $routeProvider, You may want to restructure them. Also note that config block must have a function.

First create the module, then register the controller and config:

angular
.module('main', ['ngRoute']);

and then use it or chain through, i.e

angular.module("main", ["ngRoute"]).controller("MainCtrl", [
  "$scope"
  ($scope) ->
    return $scope.test = {}
]).config [
  "$routeProvider"
  ($routeProvider) -> //Check this
    return $routeProvider.when("/",
      templateUrl: "homePage/homePage.html"
      controller: "MainCtrl"
    )
]

otherwise with the order of the script you have you are trying to create a controller on the app main before even it exists.

Also note that you need to include angular-router script as well.

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the error is that I place the right bracket in the wrong place (wrap another controller in the callback).. which can not be found in my test code above..
Yeah i do not use coffeescript, i had to use coffee2js to see that . :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.