【问题标题】:Argument 'indexController' is not a function, got undefined参数 'indexController' 不是函数,未定义
【发布时间】:2016-05-22 21:00:05
【问题描述】:

以前有人问过这个问题,但它没有回答我的问题。我对角度很陌生,我现在只是把东西放在一起。我正试图让我的工厂在我的控制器内工作。但是我在控制台中不断收到以下错误:

Argument 'indexController' is not a function, got undefined

我破坏了不同目录中的服务和控制器,我已将服务和控制器<script> 添加到 index.html 文件中。

我正在使用 IntelliJ 并使用它的插件来创建样板。

<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>

控制器:

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();



}]);

工厂服务:

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

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

            get: function(){
                return personNames;
            }

        }

    });

【问题讨论】:

    标签: javascript angularjs angularjs-controller angularjs-module


    【解决方案1】:

    您不应该在 FactoryService 中再次重新创建角度模块 myApp,当您再次创建 myApp 模块时,它会将旧的注册组件刷新到该模块。因此,当您注册 FactoryService 时,它会删除旧的注册组件,在这里它会删除 indexController 控制器。当ng-controller 指令被评估时,它会在模块内搜索indexController 控制器并抛出错误

    应该是

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

    而不是

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

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 2015-09-10
      • 2016-10-28
      • 2016-12-17
      • 2014-06-29
      • 1970-01-01
      • 2017-01-13
      • 2015-05-17
      • 1970-01-01
      相关资源
      最近更新 更多