gpt4 book ai didi

javascript - 注入(inject)工厂时 Angular 停止工作

转载 作者:行者123 更新时间:2023-11-30 16:04:44 25 4
gpt4 key购买 nike

我下定决心要将服务和工厂注入(inject)其中。当我注入(inject)一个名为 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>

最佳答案

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

.factory 是我们一般调用的方法,以名称和函数作为参数。这个函数只是一个必须返回对象的函数。如果您不返回任何内容,angular 将陷入无限摘要。

如果您使用了 .service,则不返回任何内容都可以。那是因为服务是一个构造函数。您可以在服务函数中包含 this 并向 this 添加属性。

更新

貌似在angularjs 1.3.4版本之前,没有报错。从1.3.4开始,无限摘要抛出错误。

关于javascript - 注入(inject)工厂时 Angular 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com