将数据从工厂传递到Angular Controller

时间:2017-09-26 15:04:46

标签: angularjs angularjs-controller angularjs-factory

我有一个现有的Angular Factory,我在插入一些需要将$ resource的结果传递给我的Angular Controller的新代码时遇到了一些麻烦。我正在尝试在下面显示的“sendFilesToController()”函数中执行此操作。我不确定语法。任何帮助表示赞赏。

谢谢,

皮特

我的工厂看起来像这样:

(function () {
    'use strict';

    angular
        .module('ooApp.controllers')
        .factory('fileManager', fileManager);


    fileManager.$inject = ['$q', 'fileManagerClient', 'appInfo', 'sharedPropertiesSvc', 'FileSvc', '$location', 'growl', ];

    function fileManager($q, fileManagerClient, appInfo, sharedPropertiesSvc, FileSvc, $location, growl) {
        var service = {
            files: [],
            load: load,
            upload: upload,
            remove: remove,
            fileExists: fileExists,
            pass: pass,
            status: {
                uploading: false
            }
        };

        return service;

        function load() {
            appInfo.setInfo({ busy: true, message: "loading files" })

            service.files.length = 0;

            return fileManagerClient.query()
                                .$promise
                                .then(function (result) {
                                    result.files
                                            .forEach(function (file) {
                                                service.files.push(file);
                                            });

                                    appInfo.setInfo({ message: "files loaded successfully" });

                                    return result.$promise;
                                },
                                function (result) {
                                    appInfo.setInfo({ message: "something went wrong: " + result.data.message });
                                    return $q.reject(result);
                                })
                                ['finally'](
                                function () {
                                    appInfo.setInfo({ busy: false });
                                });
        }

        function upload(files) {
            //ToDo Check that file extension exists here

                                            item.OversightId = oversightId;
                                            FileSvc.update({ Id: oversightId }, item).$promise.then(function (response) {
                                                var x = 5;


                                                sendFilesToController(id);


                                            });

                                            return result.$promise;
                                        },
                                        function (error) {
                                            appInfo.setInfo({ message: "something went wrong: " + error.data.message });
                                            sharedPropertiesSvc.setDuplicateFileSwitch(true);
                                            sharedPropertiesSvc.setDupFileMsg(error.data.error.message);

                                            var url = '/oa/?id=' + oversightId;
                                            $location.url(url);

                                            return $q.reject(error);
                                        })
                                        ['finally'](
                                        function () {
                                            appInfo.setInfo({ busy: false });
                                            service.status.uploading = false;
                                        });
        }

       function sendFilesToController(id) {
            //appInfo.setInfo({ busy: true, message: "deleting file " + file.ImageName });


            return {

                sendFiles: function () {

                    //return FileSvc.getById({ Id: oversightId }).$promise.then(function (response) {
                    //});
                    var files = {};
                    FileSvc.getById({ Id: id }).success(function (response) {
                        angular.copy(response, files);
                    });
                    return files;
                }

        }


        }

    }
})();

在我的控制器中我只有这个:

 $scope.files = fileManagerClient.sendFiles(response);

1 个答案:

答案 0 :(得分:0)

你误解了工厂和控制器的使用情况......

定义factory =>

然后注入工厂=>进入你想要的控制器

angular.module('myExample', [])
  .controller('myCtrl', function($scope, Factory) {

      console.log('Factory', Factory.word); 
  });

// factory
angular.module('myExample')
  .factory('Factory', function () {
    var factory = { };
    factory.word = 'example';
    return factory;
})