注入工厂时角度停止工作

时间:2016-05-13 19:41:51

标签: javascript angularjs

我决心将服务和工厂注入其中。当我注入一个名为ValidationMatrixFactory的工厂时,我再也无法工作了。主页加载但链接不再解析,基本上点击任何东西都没有效果。 Angular不会抛出任何错误。一切都停止了。我尝试过多个浏览器,重启IIS,一切都没有效果。

解决方案中的console.log语句永远不会被命中。以下是决心:

        resolve: {
            eventData: ['$http', '$stateParams', 'ViewMatrixService', 'ValidationMatrixFactory', function ($http, $stateParams, ViewMatrixService, ValidationMatrixFactory) {
                console.log('EventId: ' + $stateParams.eventId);
                return $http.get('http://localhost:10569/api/eventView/' + $stateParams.eventId).then(function (response) {
                    ViewMatrixService.GenerateViewMatrix(response.data);
                    ValidationMatrixFactory.GenerateValidationMatrix(response.data);
                    return response.data;
                });
            }]
        }

如果我取出对ValidationMatrixFactory的引用,一切都有效。 ValidationMatrixFactory与ViewMatrixService非常相似,因此我不确定它为什么会出现问题。

这是ValidationMatrixFactory:

angular.module('myWebApp.services')
    .factory('ValidationMatrixFactory', function() {
        var ValidationMatrixFactory = {};

        ValidationMatrixFactory.GenerateValidationMatrix = function (eventData) {
            var currentStage = eventData.CoreEvent.Stage;
            initializeValidationMatrix();
            determineRequiredFields(currentStage);
        }

        function initializeValidationMatrix() {
            ValidationMatrixFactory.EventTitleRequired = false;
            ValidationMatrixFactory.EventSummaryRequired = false;
            ValidationMatrixFactory.DesiredPublicationDateRequired = false;
            ValidationMatrixFactory.DesiredEffectiveDateRequired = false;
            ValidationMatrixFactory.ReasonForDesiredEffectiveDateRequired = false;
            ValidationMatrixFactory.AssessmentTargetDateRequired = false;
            ValidationMatrixFactory.FRSOTargetDateRequired = false;
            ValidationMatrixFactory.BLRTargetDateRequired = false;
            ValidationMatrixFactory.SSOTargetDateRequired = false;
            ValidationMatrixFactory.BLSOTargetDateRequired = false;
            ValidationMatrixFactory.FSOTargetDateRequired = false;
            ValidationMatrixFactory.PublicationTargetDateRequired = false;
            ValidationMatrixFactory.EffectiveTargetDateRequired = false;
            ValidationMatrixFactory.RMRequired = false;
            ValidationMatrixFactory.PublisherRequired = false;
            ValidationMatrixFactory.MoPRequired = false;
            ValidationMatrixFactory.PriorityRequired = false;
        }

        function determineRequiredFields(currentStage) {
            ValidationMatrixFactory.EventTitleRequired = true;
            ValidationMatrixFactory.EventSummaryRequired = true;
            ValidationMatrixFactory.ReasonForDesiredEffectiveDateRequired = true;
            ValidationMatrixFactory.DesiredPublicationDateRequired = true;
            ValidationMatrixFactory.DesiredEffectiveDateRequired = true;

            if (currentStage.StageNumber > 1) {
                ValidationMatrixFactory.AssessmentTargetDateRequired = true;
                ValidationMatrixFactory.FRSOTargetDateRequired = true;
                ValidationMatrixFactory.BLRTargetDateRequired = true;
                ValidationMatrixFactory.SSOTargetDateRequired = true;
                ValidationMatrixFactory.BLSOTargetDateRequired = true;
                ValidationMatrixFactory.FSOTargetDateRequired = true;
                ValidationMatrixFactory.PublicationTargetDateRequired = true;
                ValidationMatrixFactory.EffectiveTargetDateRequired = true;
                ValidationMatrixFactory.RMRequired = true;
                ValidationMatrixFactory.PublisherRequired = true;
                ValidationMatrixFactory.MoPRequired = true;
            }

            if (currentStage.StageNumber > 2) {
                ValidationMatrixFactory.PriorityRequired = true;
            }
        }

        ValidationMatrixFactory.ValidateFields = function (saveType) {

        }
    });

我在index.html中引用了脚本。

    <script src="libraries/angular/1.4.8/angular.min.js"></script>
    <script src="libraries/angular/1.4.8/angular-animate.min.js"></script>
    <script src="libraries/angular-ui-router/0.2.14/angular-ui-router.min.js"></script>
    <script src="libraries/angular-scroll/angular-scroll.min.js"></script>
    <script src="libraries/uiboostrap/0.14.3/ui-bootstrap-tpls-0.14.3.min.js"></script>
    <script src="libraries/dirPagination/dirPagination.js"></script>
    <script src="libraries/angular-loading-bar/loading-bar.js"></script>
    <script src="libraries/ng-file-upload/ng-file-upload.min.js"></script>
    <script src="libraries/ng-file-upload/ng-file-upload-shim.min.js"></script>
    <script src="js/app.services.js"></script>
    <script src="js/view.matrix.service.js"></script>
    <script src="js/validation.matrix.factory.js"></script>
    <script src="js/utility.service.js"></script>
    <script src="js/app.controllers.js"></script>
    <script src="partials/events/EventCtrl.js"></script>
    <script src="partials/events/EventHeaderCtrl.js"></script>
    <script src="partials/events/EventOverviewTabCtrl.js"></script>
    <script src="partials/events/EventAdditionalInformationTabCtrl.js"></script>
    <script src="partials/events/EventDSOTabCtrl.js"></script>
    <script src="partials/events/EventFooterCtrl.js"></script>
    <script src="js/nonAngular.js"></script>
    <script src="js/app.js"></script>

1 个答案:

答案 0 :(得分:2)

您必须从工厂返回ValidationMatrixFactory个对象。

.factory是一种我们通常使用名称和函数作为参数调用的方法。这个函数只是一个必须返回一个对象的函数。如果你没有返回任何东西,角度将陷入无限的消化。

如果您使用.service,则无法返回任何内容。那是因为服务是构造函数。您可以在服务功能中添加this,并将属性添加到this

<强>更新

看起来像angularjs版本1.3.4之前,没有抛出任何错误。从1.3.4开始,无限摘要会引发错误。