参数“indexController"不是函数,未定义 [英] Argument 'indexController' is not a function, got undefined
问题描述
以前有人问过这个问题,但没有回答我的问题.我对 angular 很陌生,我现在只是把东西放在一起.我试图让我的工厂在我的控制器内工作.但是,我的控制台中不断出现以下错误:
参数 'indexController' 不是函数,未定义
我破坏了不同目录中的服务和控制器,我已将服务和控制器 添加到 index.html 文件中.
我正在使用 IntelliJ 并使用其插件来创建样板.
<ul class="菜单"><li><a href="#/view1">view1</a></li><li><a href="#/view2">view2</a></li><!--[如果是 IE 7]><p class="browsehappy">您使用的是<strong>过时</strong>浏览器.请<a href="http://browsehappy.com/">升级您的浏览器</a>以改善您的体验.</p><![endif]--><div><p>名称:<input type="text" ng-model="name"></p><h1>你好{{name}}</h1>//这工作得很好
<div ng-controller="indexController">//不出现<ul><li ng-repeat="x in nameArray">{{x.name + ' 是:'+x.age+' 岁'}}
控制器:
angular.module('myApp',[]).controller('indexController', ['$scope', 'Contact', function($scope, Contact){变量名 = [{姓名:'德鲁',年龄:30},{name:'Meike', 年龄: 32},{名称:'加里',年龄:64}];Contact.add(names);$scope.nameArray = Contact.get();}]);
工厂服务:
angular.module('myApp',[]).factory('联系人', 函数 ContactFactory(){var personNames = [];返回{添加:函数(名称){personNames.add(name);},得到:函数(){返回人名;}}});
当你再次创建 myApp
模块时,你不应该在 FactoryService 中再次创建 Angular 模块 myApp
它会将旧的注册组件刷新到该模块.因此,当您注册 FactoryService
时,它会删除旧的注册组件,这里会删除 indexController
控制器.当 ng-controller
指令被评估时,它会在模块内搜索 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屋!