Argument 'indexController' is not a functi

2019-03-02 22:59发布

This has been asked before but it didn't answer my question. I am pretty new to angular and I am just putting things together at the moment. I am trying to get my factory to work inside my controller. However I keep getting the following error in my console:

Argument 'indexController' is not a function, got undefined

I am broke the services and controller in different directories and I have added the service and controller <script> to the index.html file.

I am using IntelliJ and used its plug-in to create the boilerplate.

<body >
  <ul class="menu">
    <li><a href="#/view1">view1</a></li>
    <li><a href="#/view2">view2</a></li>
  </ul>


  <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  <![endif]-->

  <div>

          <p>Name: <input type="text" ng-model="name"> </p>
          <h1>Hello {{name}}</h1> //this works just fine

  </div>

  <div ng-controller="indexController"> //doesn't appear
      <ul>
          <li ng-repeat="x in nameArray">
              {{x.name + ' is : '+x.age+' years old'}}
          </li>
      </ul>

  </div>

Controller:

angular.module('myApp',[])
    .controller('indexController', ['$scope', 'Contact', function($scope, Contact){

        var names = [
            {name:'Drew' , age: 30},
            {name:'Meike', age: 32},
            {name:'Garry', age:64}
        ];

        Contact.add(names);

    $scope.nameArray = Contact.get();



}]);

FactoryService:

angular.module('myApp',[])
    .factory('Contact', function ContactFactory(){
        var personNames = [];
        return{

            add: function(name) {
                personNames.add(name);
            },

            get: function(){
                return personNames;
            }

        }

    });

1条回答
相关推荐>>
2楼-- · 2019-03-02 23:56

You shouldn't recreate the angular module myApp again inside FactoryService, when you create myApp module again it flushes out old registered component to that module. So when you register FactoryService it removes out old register component, here it removes indexController controller. And when ng-controller directive gets evaluated it searches for indexController controller inside module and throws an error

It should be

angular.module('myApp')
    .factory('Contact', function ContactFactory(){

instead of

angular.module('myApp',[])
    .factory('Contact', function ContactFactory(){
查看更多
登录 后发表回答