AngularJS - Dynamic $StateProvider With UI-Router Views and $ocLazyLoad Resolves Syntax

前端 未结 1 1212
傲寒
傲寒 2020-12-17 07:41

I currently have a good working static version of my question being asked. The issue is that I can\'t seem to format the JSON properly when trying to use $StateProvider in a

相关标签:
1条回答
  • 2020-12-17 07:51

    I actually got this working and wanted to share the answer and code formatting in case anyone was interested in seeing a working standalone array with a loop that adds an Abstract state and child states with views that use resolves that lazy load controllers and files dynamically before the route is loaded via the $ocLazyLoad module.

    I figured the code alone might help someone out there struggle less than I did.

    var states = [
        { "name": "app", "abstract": true, "url": "", "templateUrl": "app/components/core/layout/layout.html", "controller": "LayoutCtrl" },
        {
            "name": "app.dashboard",
            "views": {
                "feature": {
                    "templateUrl": "lazyload/components/core/features/dashboard/dashboard.html",
                    "controller": "DashboardCtrl as dashboard",
                    "resolve": {
                        "dashboard": function ($ocLazyLoad) {
                            return $ocLazyLoad.load(
                                {
                                    "name": "dashboard",
                                    "files": ["lazyload/components/core/features/dashboard/dashboard-controller.js"]
                                }
                            )
                        }
                    }
                }
            }
        },
        {
            "name": "app.associations_hospital-surgeon",
            "views": {
                "feature": {
                    "templateUrl": "lazyload/components/core/features/other-feature/other-feature.html",
                    "controller": "OtherFeatureCtrl as otherFeature",
                    "resolve": {
                        "otherFeature": function ($ocLazyLoad) {
                            return $ocLazyLoad.load(
                                {
                                    "name": "otherFeature",
                                    "files": ["lazyload/components/core/features/other-feature/other-feature.js"]
                                }
                            )
                        }
                    }
                }
            }
        }
    ];
    
    angular.forEach(states, function (state) {
        console.log('state --------------------------------------------------------------------------');
        console.log(state);
        $stateProvider.state(state.name, state);
    });
    

    And here I decorate the JSON so it can be loaded from a server, but since functions aren't valid in JSON files this seemed to work for me using custom data properties used to define the function attached when used. This allowed me to load the file externally or from a server and still use the lazyloading via $ocLazyLoad as a function when needed.

    var states = [
        { "name": "app", "abstract": true, "url": "", "templateUrl": "app/components/core/layout/layout.html", "controller": "LayoutCtrl" },
        {
            "name": "app.dashboard",
            "views": {
                "feature": {
                    "templateUrl": "lazyload/components/core/features/other-feature/other-feature.html",
                    "controller": "DashboardCtrl as dashboard",
                    "resolve": {},
                    "data": {
                        "controllerAlias": "dashboard",
                        "controllerFiles": ["lazyload/components/core/features/other-feature/other-feature.js"]
                    }
                }
            }
        },
        {
            "name": "app.associations_hospital-surgeon",
            "views": {
                "feature": {
                    "templateUrl": "lazyload/components/core/features/associations/other-feature.html",
                    "controller": "OtherFeatureCtrl as otherFeature",
                    "resolve": {},
                    "data": {
                        "controllerAlias": "otherFeature",
                        "controllerFiles": ["lazyload/components/core/features/other-feature/other-feature.js"]
                    }
                }
            }
        }
    ];
    
    angular.forEach(states, function (state) {
        console.log('state --------------------------------------------------------------------------');
        try{
            console.log(state.views.feature.resolve);
            state.views.feature.resolve[state.views.feature.data.controllerAlias] = function($ocLazyLoad){return $ocLazyLoad.load({"name": state.views.feature.data.controllerAlias,"files": state.views.feature.data.controllerFiles})};
        }catch(e){
    
        }
    
        $stateProvider.state(state.name, state);
    });
    
    0 讨论(0)
提交回复
热议问题