1

I dont understand why TestCntrl is undefined when I run this. I already have an existing controller (MainCntrl) in the parent and now I want the child to have its own controller (TestCntrl).

(function() {
  'use strict';
  angular
    .module('app')
      .config(function ($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) {

        $urlMatcherFactoryProvider.strictMode(false);
        $urlRouterProvider.when('/', '/search'); 
        $urlRouterProvider.when('/search/name', '/search');

        $urlRouterProvider.otherwise('/');

        $stateProvider
            .state('index', {
              url: '/',
              abstract: true,

              templateUrl: 'app/layout/layout.html',
              controller: 'MainController',
              controllerAs: 'main'

            })

            .state('index.layout', {

                initFactory2: ['initFactory', function(initFactory) {

                    return initFactory.getClasses().then(function(data) {
                        return data.data;
                    });
                }]

              },
              url: 'search',
              views: {

                'form@index': {
                  templateUrl: 'app/partials/form.html',
                  controller: 'TestController as test'
},
                'results@index':{}
              }
            })
})();

TestController

(function () {
    'use strict';

    angular
        .module('app')
        .controller('TestController', TestController);

    /** @ngInject */
    function TestController($scope, $stateParams, $state, model,
                             initFactory2) {
        var vm = this;

        vm.$scope = $scope;
    }
 });

Main Controller

(function () {
    'use strict';

    angular
        .module('app')
        .controller('MainController', MainController);

    /** @ngInject */
    function MainController($scope, $stateParams, $state, $timeout, model,
                            SearchFactory) {
        var vm = this;
    }
})();

This is the error I'm getting:

Error: [ng:areq] Argument 'TestController' is not a function, got undefined
http://errors.angularjs.org/1.3.17/ng/areq?p0=TestController&p1=not%20a%20function%2C%20got%20undefined
5
  • Are the parenthesis at the end of the TestController missing from your actual code or is that just a copy-paste error? Commented Aug 11, 2015 at 13:37
  • @MatthewGreen I've fixed that . Copy/paste error Commented Aug 11, 2015 at 13:59
  • @MatthewGreen youre right. I added it to my code and that did the trick. Feel free to answer and I'll accept as answer. Commented Aug 11, 2015 at 14:12
  • If that was actually the issue you may want to roll back your edit so that people can see what the original issue was. Without it the answer looks a little out of place. Commented Aug 11, 2015 at 14:15
  • @MatthewGreen ok i've undone my edits. Thank you! Commented Aug 11, 2015 at 14:16

2 Answers 2

1

You are missing your parenthesis at the end of your IIFE on your TestController. That is needed to properly declare it as function to be executed. Without it, your code for the TestController is never called and therefore never instantiated.

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

Comments

0

the problem is with your TestController

(function () {
'use strict';

   angular
    .module('app')
    .controller('TestController', TestController);

  /** @ngInject */
  function TestController($scope, $stateParams, $state, model, initFactory2) {
    var vm = this;
    vm.$scope = $scope;
   }
});

just, try reversing the order

(function () {
 'use strict';
  /** @ngInject */
  var TestController = function TestController($scope, $stateParams, $state, model, initFactory2){
    var vm = this;
    vm.$scope = $scope;
  };
  angular
    .module('app')
    .controller('TestController', TestController);
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.