Angularjs controller not working, throws error (newbie)

leandronn

i'm working with angular-seed project. I'm trying to retreive data from mysql database. I need to know how to define different controller for each view. For example, I have this structure:

js
|_modules
  |_companies
     |_controller.js
     |_data.js
|_app.js
|_base.js

I have added this route to app.js

.state('app.companies', {
    url: '/companies',
    title: 'Companies',
    templateUrl: helper.basepath('companies.html'),
    controller: 'companiesCtrl' //THIS THROWS THE ERROR BELOW
})

companies.html has scripts added to botom of the page

<script src="app/js/modules/companies/data.js"></script>
<script src="app/js/modules/companies/controller.js"></script>

and this is the code for controller.js (also tested the commented part)

(function() {
    'use strict';

    angular
        .module('appname')
        .controller('companiesCtrl', companiesCtrl);

    companiesCtrl.$inject = ['$scope','companiesData','$log'];
    function companiesCtrl($scope, companiesData, $log) {
        console.log('asd'); //NEVER REACH THIS LOG
    };
});

/*var app = angular
.module('appname')
.controller('companiesCtrl', ['$scope','companiesData','$log', function($scope, companiesData, $log){
console.log('asd'); //NEVER REACH THIS LOG
$scope.companies = {};
Data.get('companies').then(function(data){
    $scope.companies = data.data;
    console.log('($scope.companies)');
});
}]);
*/

But I keep getting

Error: [ng:areq] Argument 'companiesCtrl' is not a function, got undefined

Same if I script ng-controller="companiesCtrl" on my view.

leandronn

The problem was on script loading. I had to use lazy loading of the files within my app.js Here's the code:

app.js companies route

.state('app.companies', {
          url: '/companies',
          title: 'Companies',
          templateUrl: helper.basepath('companies.html'),
          resolve: helper.resolveFor('companiesCtrl'),
          controller: 'companiesCtrl'
      })

lazy load code

.constant('APP_REQUIRES', {
      scripts: {
        'modernizr':          ['vendor/modernizr/modernizr.custom.js'],
        'icons':              ['vendor/fontawesome/css/font-awesome.min.css',
                               'vendor/simple-line-icons/css/simple-line-icons.css'],
        'companiesCtrl':      ['app/js/modules/companies/data.js','app/js/modules/companies/controller.js']
      },
      modules: []
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Newbie trouble with AngularJS controller and view databinding problems

From Dev

Injecting multiple Services to Controller throws error- AngularJs

From Dev

Injecting multiple Services to Controller throws error- AngularJs

From Dev

Angularjs: ui-router controller in views throws error

From Dev

AngularJS Controller not working in nested Controller

From Dev

AngularJS greetController Controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

multiple controller not working in angularjs

From Dev

AngularJS controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

AngularJS greetController Controller not working

From Dev

AngularJS - Toaster not working in Controller

From Dev

Multiple controllers in angularJS throws error

From Dev

Why is my AngularJS controller definition and markup not working as expected? - <controller> is not a function error

From Dev

Angularjs controller is showing error

From Dev

Error in registering the controller AngularJS

From Dev

Angularjs: Controller Register Error

From Dev

WKWebview evaluateJavascript is not working, throws an error

From Dev

Angularjs - data binding not working with controller

From Dev

AngularJS ng-controller not working

From Dev

Angularjs $setPristine not working with controller as syntax

From Dev

Show and Hide Controller not working, AngularJS

From Dev

Simple AngularJS Controller Demo Not Working

From Dev

laravel throws error when returning ->first() in controller

From Dev

MVC Controller throws null reference error

From Dev

laravel throws error when returning ->first() in controller

From Dev

jQuery not working for a newbie

From Dev

AngularJS Error: [$controller:ctrlreg] Controller is not registered

From Dev

AngularJS with Karma: $controller not defined error

Related Related

  1. 1

    Newbie trouble with AngularJS controller and view databinding problems

  2. 2

    Injecting multiple Services to Controller throws error- AngularJs

  3. 3

    Injecting multiple Services to Controller throws error- AngularJs

  4. 4

    Angularjs: ui-router controller in views throws error

  5. 5

    AngularJS Controller not working in nested Controller

  6. 6

    AngularJS greetController Controller not working

  7. 7

    angularjs 1.3 controller as not working

  8. 8

    multiple controller not working in angularjs

  9. 9

    AngularJS controller not working

  10. 10

    angularjs 1.3 controller as not working

  11. 11

    AngularJS greetController Controller not working

  12. 12

    AngularJS - Toaster not working in Controller

  13. 13

    Multiple controllers in angularJS throws error

  14. 14

    Why is my AngularJS controller definition and markup not working as expected? - <controller> is not a function error

  15. 15

    Angularjs controller is showing error

  16. 16

    Error in registering the controller AngularJS

  17. 17

    Angularjs: Controller Register Error

  18. 18

    WKWebview evaluateJavascript is not working, throws an error

  19. 19

    Angularjs - data binding not working with controller

  20. 20

    AngularJS ng-controller not working

  21. 21

    Angularjs $setPristine not working with controller as syntax

  22. 22

    Show and Hide Controller not working, AngularJS

  23. 23

    Simple AngularJS Controller Demo Not Working

  24. 24

    laravel throws error when returning ->first() in controller

  25. 25

    MVC Controller throws null reference error

  26. 26

    laravel throws error when returning ->first() in controller

  27. 27

    jQuery not working for a newbie

  28. 28

    AngularJS Error: [$controller:ctrlreg] Controller is not registered

  29. 29

    AngularJS with Karma: $controller not defined error

HotTag

Archive