在调用工厂时,我一直收到“未定义的不是函数”的提示。我使用angular-seed作为我的设置框架。由于某些原因,它不会将"GetFormVals“识别为有效的工厂。
在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);
});在controllers.js中:
function ctrlFSR($scope, $http, $location, GetFormVals) {
$scope.test = GetFormVals();
}
ctrlFSR.$inject = ['$scope','$location'];在services.js中:
'use strict';
/* Services */
angular.module('myApp.services', []).
factory('GetFormVals', function(){
return "test";
});我必须错过一些简单的东西。
jsfiddle:http://jsfiddle.net/wUjw5/11/
发布于 2013-01-04 05:38:45
您需要列出要注入到控制器中的所有依赖项:
function ctrlFSR($scope, $http, $location, GetFormVals) {
$scope.test = GetFormVals();
}
ctrlFSR.$inject = ['$scope', '$http', '$location', 'GetFormVals'];发布于 2013-01-04 05:29:48
将您的服务更改为
'use strict';
/* Services */
angular.module('myApp.services', []).
factory('GetFormVals', function(){
return {
exampleValue: "test5"
}
});然后,在您的控制器中,将其更新为:
function ctrlFSR($scope, $http, $location, GetFormVals) {
$scope.test = GetFormVals.exampleValue;
}
ctrlFSR.$inject = ['$scope','$location'];我认为混淆是因为你将GetFormVals作为一个函数来调用,而实际上它是一个服务。
jsfiddle:http://jsfiddle.net/rtCP3/49/
修改你的js http://jsfiddle.net/wUjw5/12/
不要使用ctrlFSR.$inject = '$scope','$location';进行注入。你已经是了。
https://stackoverflow.com/questions/14147486
复制相似问题