【问题标题】:The ui router resolve result data is not injected in the controllerui 路由器解析结果数据未注入控制器
【发布时间】:2014-08-10 04:48:05
【问题描述】:

在我的 SchoolyearController 中,参数 schoolyears 未定义。

如何在 schoolyearService 中检索我的学年对象并将结果注入 SchoolyearController?

服务

'use strict';
angular.module('schoolyear').service('schoolyearService', function ($http) {

    return {
        getSchoolyears: function () {            
            var path = 'scripts/model/schoolyears.json';
            $http.get(path).then(function (response) {
                return response.data.schoolyears;  // The schoolyears here are the 6 expected JS objects in an array, so far so good but how do I get those objects into the SchoolyearController as parameter ?
            });
        }
    };
});

UI-ROUTER

resolve: {
    schoolyearService: ['schoolyearService',
        function (schoolyearService) {
            return schoolyearService.getSchoolyears();
        }]
},

控制器

'use strict';
angular.module('schoolyear').controller('SchoolyearController', function ($scope, schoolyears) {

    $scope.schoolyears = schoolyears; // I do not want to do a $http request here I just want to get passed the data here !!!
});

更新

resolved 属性中的学年仍然未定义,为什么?

工厂

'use strict';
angular.module('schoolyearModule').factory('schoolyearFactory', function ($http) {

    return {
        getSchoolyears: function () {
            var path = 'scripts/model/schoolyears.json';
            $http.get(path).then(function (response) {
                return response.data.schoolyears;  // The schoolyears here are the 6 expected JS objects in an array
            });
        }
    };
});

UI-ROUTER

resolve: {
    schoolyears: function(schoolyearFactory) {
        var schoolyears = schoolyearFactory.getSchoolyears();
        return schoolyears;
    }

},

控制器

'use strict';
angular.module('schoolyearModule').controller('ProjectsController', function ($scope, schoolyears) {

    $scope.schoolyears = schoolyears; // I do not want to do a $http request here I just want to get passed the data here !!!
});

【问题讨论】:

    标签: angularjs angular-ui-router angularjs-http


    【解决方案1】:

    您的解析值被命名为schoolyearService(因此与具有相同名称的服务发生冲突):

    resolve: {
        schoolyearService: ...
    

    但您尝试使用名称schoolyears 注入它:

    angular.module('schoolyear').controller('SchoolyearController', 
        function ($scope, schoolyears) {
    

    在任何地方都使用相同的名称 (schoolyears):

    resolve: {
        schoolyears: ...
    

    另外,你应该使用 factory() 方法来定义你的服务,而不是servoce() 方法。 service() 方法将构造函数作为参数,而不是返回作为实际服务实例的对象的函数。

    编辑:

    此外,您不会从 getSchoolyears() 服务方法返回任何内容。所以undefined 被返回。你需要的是:

        getSchoolyears: function () {
            var path = 'scripts/model/schoolyears.json';
            return $http.get(path).then(function (response) {
                return response.data.schoolyears;
            });
        }
    

    【讨论】:

    • 我已经用更正的代码 sn-ps 更新了我的问题,但 schoolyearFactory 仍然返回未定义的学年对象。在 SchoolyearFactory 本身中,response.data.schoolyears 对象是我作为测试数据放入 schoolyears.json 文件中的 6 个 JS 对象。
    • 好的,我认为它足以调用 $http 并且只在响应回调中返回......谢谢它的工作!
    猜你喜欢
    • 2015-03-10
    • 1970-01-01
    • 2022-06-22
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多