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

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フィドルは http://jsfiddle.net/wUjw5/12/を変更しました

ctrlFSRで注入しないでください。$inject= ['$ scope'、'$location'];。あなたはすでにそうです。

于 2013-01-03T21:29:48.143 に答える