注入解决方案

时间:2013-12-20 12:22:58

标签: angularjs angular-ui-router resolve

我想知道,如果我可以将resolve参数注入另一个resolve参数。代码会告诉你更多:

.state('videos.videosLection', {
        url : '/:lection',
        templateUrl : '/partials/videosLection.html',
        controller : 'VideosLectionCtrl',
        menuItem : 'Videos',
        resolve : {
                selectedLection : function ($stateParams, lections) {
                        ...
                },
                sections : function(Section, selectedLection) {
                        ...
                }                   
         }
})

我正在使用角度UI路由器进行路由。并且我需要在启动部分解析之前使用selectedLection。

有什么想法吗?非常感谢

2 个答案:

答案 0 :(得分:0)

您可以将selectedLection件转变为服务吗?然后做这样的事情:

resolve: {
    sections: function(Section, selectedLectionService, $stateParams, lections){
        var result = selectedLectionService($stateParams, lections);
        // do something with result here...
    }
}

我不知道这是否能满足您的所有需求,但也许它会让您指向正确的方向。如果不能解决问题,请告诉我。

答案 1 :(得分:0)


我终于弄清楚了,这里的方法是只解决一个具有几个属性的对象,这些属性本身就是承诺。检查代码:

resolve : {
    data : function($stateParams, lections, Video, Section, $q) {
        // defer for the data object
        var defer = $q.defer();

        // selectedLection
        var selectedLection = null;

        // if there is lection selected
        if($stateParams.lection)
        {
            // lection is selected
            lections.forEach( function(lection)
            {
                // find the selected lection
                if(lection.addressUrl == $stateParams.lection)
                {
                    // save it
                    selectedLection = lection;
                }
            });
        }
        else
        {
            // resolve it with everything empty
            defer.resolve({
                selectedLection : null,
                lectionVideos : [],
                sections: []
            });
            return defer.promise;
        }

        // promise to the lection videos
        var deflectionvideos = $q.defer();
        // find all the videos by the lection id
        Video.GetAllByLection(selectedLection.id, null, function(data) {
            // resolve it with the data
            deflectionvideos.resolve(data);
        }, function(error) {
            // resolve it with empty array
            deflectionvideos.resolve([]);
        });

        // promise for the sections
        var defsections = $q.defer();
        // find all the sections of selected lectino
        Section.GetAllByLection(selectedLection.id, function(data) {
            // resolve it with the data
            defsections.resolve(data);
        }, function(error) {
            // resolve it with empty array
            defsections.resolve([]);
        });

        // wait, untill all the promises for sections and lectionsvideos are resolved
        $q.all([
                deflectionvideos.promise,
                defsections.promise
            ]).then(function(data) {
                // resolve it with actual data
                defer.resolve({
                    selectedLection : selectedLection,
                    lectionVideos : data[0],
                    sections: data[1]
                });
            });

        // return promise for the data object
        return defer.promise;
    }
}

如果你们有其他方法或更好的解决方案,请告诉我: - )

希望有人帮助