Simran Kaur
Simran Kaur

Reputation: 1101

can not read property controller of undefined

In index.html, I have added path to controller file.

I am getting this error with controller. Even if I comment out the code in controller.js, I still get this error.

controller.js

angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope,$state,$stateParams,$rootScope){

});

app.js

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'starter.controllers'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
      .state('navBar', {
        url: "/navBar",
        abstract: true,
        templateUrl: "templates/navigationBar.html",
        controller:'AppCtrl'

      })
        .state('navBar.orderReceive', {
        url: "/orderReceive",
        views: {
          'orderReceiveView': {
            templateUrl: "templates/OrderReceiveChoice.html"


          }
        }
      })

        $urlRouterProvider.otherwise(function ($injector) {
           var $state = $injector.get('$state');
            $state.go('navBar.orderReceive');
        });

})

But I get an error Error:

TypeError: Cannot read property 'controller' of undefined
    at IonicModule.controller.self.update (ionic.bundle.js:51695)
    at IonicModule.controller.self.beforeEnter (ionic.bundle.js:52135)
    at IonicModule.controller.self.beforeEnter (ionic.bundle.js:54079)
    at Scope.$get.Scope.$emit (ionic.bundle.js:24919)
    at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.emit (ionic.bundle.js:50634)
    at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.transition (ionic.bundle.js:50492)
    at ionic.bundle.js:52121
    at ionic.bundle.js:50370
    at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.render (ionic.bundle.js:50459)
    at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.init (ionic.bundle.js:50369)(anonymous function) @ ionic.bundle.js:21157$get @ ionic.bundle.js:17936$get.Scope.$emit @ ionic.bundle.js:24921IonicModule.factory.ionicViewSwitcher.create.switcher.emit @ ionic.bundle.js:50634IonicModule.factory.ionicViewSwitcher.create.switcher.transition @ ionic.bundle.js:50492(anonymous function) @ ionic.bundle.js:52121(anonymous function) @ ionic.bundle.js:50370IonicModule.factory.ionicViewSwitcher.create.switcher.render @ ionic.bundle.js:50459IonicModule.factory.ionicViewSwitcher.create.switcher.init @ ionic.bundle.js:50369IonicModule.controller.self.render @ ionic.bundle.js:52115IonicModule.controller.self.register @ ionic.bundle.js:52073updateView @ ionic.bundle.js:57485IonicModule.directive.compile @ ionic.bundle.js:57469invokeLinkFn @ ionic.bundle.js:17477nodeLinkFn @ ionic.bundle.js:16977compositeLinkFn @ ionic.bundle.js:16368nodeLinkFn @ ionic.bundle.js:16972compositeLinkFn @ ionic.bundle.js:16368nodeLinkFn @ ionic.bundle.js:16972compositeLinkFn @ ionic.bundle.js:16368nodeLinkFn @ ionic.bundle.js:16972compositeLinkFn @ ionic.bundle.js:16368publicLinkFn @ ionic.bundle.js:16243IonicModule.controller.self.appendViewElement @ ionic.bundle.js:52259IonicModule.factory.ionicViewSwitcher.create.switcher.render @ ionic.bundle.js:50449IonicModule.factory.ionicViewSwitcher.create.switcher.init @ ionic.bundle.js:50369IonicModule.controller.self.render @ ionic.bundle.js:52115IonicModule.controller.self.register @ ionic.bundle.js:52073updateView @ ionic.bundle.js:57485(anonymous function) @ ionic.bundle.js:57462$get.Scope.$broadcast @ ionic.bundle.js:24992$state.transitionTo.$state.transition.resolved.then.$state.transition @ ionic.bundle.js:44836processQueue @ ionic.bundle.js:23394(anonymous function) @ ionic.bundle.js:23410$get.Scope.$eval @ ionic.bundle.js:24673$get.Scope.$digest @ ionic.bundle.js:24484$get.Scope.$apply @ ionic.bundle.js:24778done @ ionic.bundle.js:19191completeRequest @ ionic.bundle.js:19363requestLoaded @ ionic.bundle.js:19304

Edit:As per the suggestion, I changed to this but I still get the same error. infact,if I remove the whole controller part, I still do if I have starter.controllers injected in app.js.

angular.module('starter.controllers', [])
.controller('AppCtrl', ['$scope', '$state','$stateParams','$rootScope', function($scope,$state,$stateParams,$rootScope){
$scope.data = [
{name: 'Giani, Pitampura', _id: '01', city:'Delhi'},
{name: 'Giani, Rajouri', _id: '02', city:'Delhi'}, 
 {name: 'Dominoz, Mayur Vihar ', _id: '03', city:'Delhi'}, 
{name: 'Dominoz, Andheri east', _id: '04', city:'Mumbai'}, 
{name: 'Dominoz, Andheri west', _id: '05', city:'Mumbai'},
{name: 'Pizaa planet, west road', _id: '06', city:'Kolkata'}, 
{name: 'Food panda, Gurgaon', _id: '07', city:'Haryana'}
];

$scope.getOutlets=function(cityname){
    console.log("called")
    $scope.outlets = data.map(function(obj){
     return obj.city === cityname
    })

}
}]);

Edit 2:

I just realized that I am getting this because of the nested state somehow. If I make the state non abstract and attach controller, it works fine.How do I retain the nested state without having this problem?

Upvotes: 0

Views: 8566

Answers (1)

Pradeep
Pradeep

Reputation: 90

Look Below reference for your answer, Looks like you are not using the dependancy array notation properly.

Reference link

Upvotes: 1

Related Questions