3

调用工厂时,我不断收到“未定义不是函数”。我使用 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/

4

2 回答 2

3

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'];
于 2013-01-03T21:38:44.880 回答
1

将您的服务更改为此

'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 fiddle 修改了 http://jsfiddle.net/wUjw5/12/

Don't inject with ctrlFSR.$inject = ['$scope','$location'];. You already are.

于 2013-01-03T21:29:48.143 回答