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 createmyApp
module again it flushes out old registered component to that module. So when you registerFactoryService
it removes out old register component, here it removesindexController
controller. And whenng-controller
directive gets evaluated it searches forindexController
controller inside module and throws an errorIt should be
instead of