I keep getting "undefined is not a function" when calling the factory. I am using the angular-seed as the framework for my setup. For some reason, it doesn't recognize "GetFormVals" as a valid factory.
In app.js:
var app = angular.module('myApp', ['myApp.filters',
'myApp.services', 'myApp.directives'], function($routeProvider, $locationProvider) {
//route declarations
$routeProvider.when('/fsr', {
templateUrl: 'partials/fsr.html',
controller: ctrlFSR
});
$locationProvider.html5Mode(true);
});
In controllers.js:
function ctrlFSR($scope, $http, $location, GetFormVals) {
$scope.test = GetFormVals();
}
ctrlFSR.$inject = ['$scope','$location'];
In services.js:
'use strict';
/* Services */
angular.module('myApp.services', []).
factory('GetFormVals', function(){
return "test";
});
I have to be missing something simple.
jsfiddle: http://jsfiddle.net/wUjw5/11/
You need to list all the dependencies to be injected into your controller:
function ctrlFSR($scope, $http, $location, GetFormVals) {
$scope.test = GetFormVals();
}
ctrlFSR.$inject = ['$scope', '$http', '$location', 'GetFormVals'];
Change your service to this
'use strict';
/* Services */
angular.module('myApp.services', []).
factory('GetFormVals', function(){
return {
exampleValue: "test5"
}
});
Then in your controller you would update it to this:
function ctrlFSR($scope, $http, $location, GetFormVals) {
$scope.test = GetFormVals.exampleValue;
}
ctrlFSR.$inject = ['$scope','$location'];
I think the confusion came in with you calling GetFormVals as a function, while in reality it's a service.
jsfiddle: http://jsfiddle.net/rtCP3/49/
your js fiddle modified http://jsfiddle.net/wUjw5/12/
Don't inject with ctrlFSR.$inject = ['$scope','$location'];. You already are.