johnpapa / angular-styleguide

Angular Style Guide: A starting point for Angular development teams to provide consistency through good practices.
http://johnpapa.net
MIT License
23.91k stars 4.16k forks source link

A1 - How to pass object received with $routeProvider to other route #848

Closed francisrod01 closed 6 years ago

francisrod01 commented 6 years ago

I tried to pass data object list to other route without search again this data. I have been trying this with $routeProvider as below:

In my controller I do it:

/**
         * Go to url with optional data object.
         *
         * @param _url
         * @param _obj
         */
        function goTo(_url, _obj) {
            let _itemObj = {}

            if (angular.isObject(_obj)) {
                _itemObj = {itemObj: _obj}
            }

            return $location.path(_url, _itemObj)
        }

in config route:

function getRoutes() {
        return [
            {
                url: '/services/:slug',
                config: {
                    templateUrl: 'app/widgets/services/item.html',
                    controller: 'myLastServicesCtrl',
                    controllerAs: 'vm',
                    params: {
                        itemObj: null
                    }
                }
            }
        ]
    }

RouterHelperProvider.js

...
function configureRoutes(routes) {
            routes.forEach(function (route) {
                const resolveAlways = routerHelperConfig.config.resolveAlways;
                route.config.resolve = route.config.resolve || {};
                route.config.resolve = angular.extend(route.config.resolve, resolveAlways);
                $routeProvider.when(route.url, route.config);
            });
            $routeProvider.otherwise({redirectTo: '/'});
        }
...

But I don't know how can this following this styleguide. Please help me.

francisrod01 commented 6 years ago

I solved this changing $routeProvider.when() to $stateProvider.state() and changing all routes in all config.route files. Also it's necessary to create a new parameter in routerHelper provider to initialize $stateProvider.

References:

Angular 1 style guide

Angular ui router passing data between states without URL

Angular ui activating a state