0

そのため、Angular を使用して小さなファクトリを作成し、ローカルの json ファイルを取得しました。そのデータをコントローラに渡したいのですが、ファクトリ名が見つからず、「未解決の変数」と表示されます。

これが、今のところ関連していると思われるコードのスニペットです。

(function () {

    var app = angular.module('locatieTool', ['ngRoute']);

    app.controller('teamController', function ($scope) {
        function init () {
            dataFactory.getTeams().success(function(data) {
                $scope.teams = data
            });
        }
        init();
        console.log($scope.teams);
    });

    // factory
    app.factory('dataFactory', function($http) {
        var team = {};

        //get local data
        team.getTeams = function() {
            return $http.get ('http://localhost:4040/');
        };
        return team;
    });

})();

私の目標は、データでできることよりも、$scope.teams をコンソール ログに記録することです。

4

2 に答える 2

2

コントローラー内に「dataFactory」を含める必要があります

(function () {
    var app = angular.module('locatieTool', ['ngRoute']);

    app.controller('teamController', function ($scope, dataFactory) {
        function init () {
            dataFactory.getTeams().success(function(data) {
                $scope.teams = data
            });
        }
        init();
        console.log($scope.teams);
    });

    // factory
    app.factory('dataFactory', function($http) {
        var team = {};

        //get local data
        team.getTeams = function() {
            return $http.get ('http://localhost:4040/');
        };
        return team;
    }); })();
于 2015-11-17T12:59:06.453 に答える
0

工場をコントローラーに渡す必要があると思います:

app.controller('teamController', function ($scope, dataFactory) {
    function init () {
        dataFactory.getTeams().success(function(data) {
            $scope.teams = data
        });
    }
    init();
    console.log($scope.teams);
});
于 2015-11-17T12:59:12.687 に答える