0

I am unable to understand the errors of Angular JS. I am trying to build a factory but it keeps on giving me the following error in firefox console.

Error: [ng:areq] http://errors.angularjs.org/1.2.9/ng/areq?p0=hospitalController&p1=not%20a%20function%2C%20got%20undefined

My Code is

index

<div class="main ng-scope" ng-view="">

partial

<button data-ng-click="ShowStaff()">show</button>

app.js

var myApp = angular.module('myApp', [
  'ngRoute',
  'artistControllers'
]);

myApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  when('/list', {
    templateUrl: 'partials/list.html',
    controller: 'ListController'
  }).
  when('/hospital', {
    templateUrl: 'partials/hospital.html',
    controller: 'hospitalController' 

  }).
  when('/docter', {
    templateUrl: 'partials/docters.html',
    controller: 'docterController'
  }).
  when('/details/:itemId', {
    templateUrl: 'partials/details.html',
    controller: 'DetailsController'
  }).
  otherwise({
    redirectTo: '/hospital'
  });
}]);

controller.js

var artistControllers = angular.module('artistControllers', ['ngAnimate']);
artistControllers.controller('ListController', ['$scope', '$http', function($scope, $http) {
  $http.get('js/data.json').success(function(data) {
    $scope.artists = data;
    $scope.artistOrder = 'name';
  });

// Starting Factory for Doctor and hospital relationship
artistControllers.factory( 'StaffFactory','$http',function(){
    var factory = {};
    $http.get('js/hospital.json').success(function(data) {
    factory.hospitals = data;
    //$scope.hospitalOrder = 'name';
    });

    $http.get('js/docters.json').success(function(data) {
    factory.doctors = data;
    //$scope.hospitalOrder = 'name';
    });

    factory.getDocs = function(){
      return factory.doctors;
      };

      factory.getHos= function(){
      return factory.hospitals;
      };
    factory.getStaff = function(){
       var result=[];
       var endres=[];
        angular.forEach(factory.hospitals, function(hospital){
          result=[];
          angular.forEach(factory.doctors,function(doc){
              if(doc.id==hospital.id)
              {
                result.push(doc);  
              }
            });
      endres.push([hospital,result]);          
      });
    return endres;
      }

    return factory;
    });     
artistControllers.SimpleController=function($scope,StaffFactory){

  $scope.customers=[];
  $scope.hospitals=[ ]; 
  $scope.doctors=[]; 
  $scope.staff=[]; 
  init();
  function init()
  {
    $scope.doctors=StaffFactory.getDocs();
    $scope.hospitals=StaffFactory.getHos();
  } 

  $scope.ShowStaff = function()
  {
    $scope.staff=StaffFactory.getStaff();
  }
  };


// Ending Factory for Doctor and hospital relationship
}]);

2 Answers 2

3

In addition to the actual error explained by @dave, if you want eror messages to be more explicit without having to follow a link, you should use angular.js instead of angular.min.js (the minimized one) for your development environment.

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

1 Comment

Yea i have added angular.js Now the errors are quite understandable.
2

If you follow the link in the error, you will see

Argument 'hospitalController' is not a function, got undefined

It sounds like you have in your html somewhere:

ng-controller="hospitalController"

but you haven't created a controller with that name.

2 Comments

I also think it is possible that in your routes, controller has not been registered with the view.
Thanks for your reply. Its not from html actually its from my app.js where i have defined my routes.

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.