参数“indexController的”不是一个函数,得到了不确定 [英] Argument 'indexController' is not a function, got undefined

查看:278
本文介绍了参数“indexController的”不是一个函数,得到了不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经被问过,但没有回答我的问题。我是pretty新的角度,我就在此刻把东西放在一起。我试图让我的厂我的控制器内工作。不过,我不断收到以下错误在我的控制台:

 参数indexController的不是一个函数,得到了不确定

我打破了在不同的目录服务和控制器和我已经加入了服务和控制<脚本方式> 来index.html文件

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

 <身体GT;
  < UL类=菜单>
    <立GT;< A HREF =#/视图1>厂景< / A>< /李>
    <立GT;< A HREF =#/视图2>&视图2 LT; / A>< /李>
  < / UL>
  <! - [如果LT IE 7>
      < p =类browsehappy>您使用的是<强>&过时LT; / STRONG>浏览器。请< A HREF =htt​​p://browsehappy.com/>升级浏览器< / A>改善您的体验与LT; / P>
  百分比抑制率ENDIF] - GT!;  < D​​IV>          < P>名称:LT;输入类型=文本NG模型=名> &所述; / P>
          < H1>您好{{名}}< / H1> //这个工作得很好  < / DIV>  < D​​IV NG控制器=indexController的> //不会出现
      < UL>
          <李NG重复=X在nameArray>
              {{x.name +'是:'+ x.age +'岁'}}
          < /李>
      < / UL>  < / DIV>

控制器:

  angular.module('对myApp',[])
    .controller('indexController的',['$范围','联系',函数($范围,联系方式){        变种名称= [
            {名:德鲁,年龄:30},
            {名称:美克,年龄:32},
            {名:加里,年龄:64}
        ];        Contact.add(地名);    $ scope.nameArray = Contact.get();}]);

FactoryService:

  angular.module('对myApp',[])
    .factory('联系',功能ContactFactory(){
        变种personNames = [];
        返回{            添加:函数(名称){
                personNames.add(名);
            },            得到:函数(){
                返回personNames;
            }        }    });


解决方案

您不应该重新角模块对myApp 里面再次FactoryService,当你创建对myApp 模块它再次老注册的组件刷新到该模块。所以,当你注册 FactoryService 它删除旧的寄存器组成部分,在这里它删除 indexController的控制器。当 NG-控制器指令被评估它搜索 indexController的内部模块控制器并引发错误

这应该是

  angular.module('对myApp')
    .factory('联系',功能ContactFactory(){

而不是

  angular.module('对myApp',[])
    .factory('联系',功能ContactFactory(){

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

        }

    });

解决方案

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

这篇关于参数“indexController的”不是一个函数,得到了不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆