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 回答