0

I am passing information using $state->data
For example:

angular
     .module('myapp', [...])
     .config(function ($stateProvider, $urlRouterProvider) {

        $urlRouterProvider.otherwise('/account');

        $stateProvider
            .state('account', {
                abstract: true,
                url: '/account',
                templateUrl: 'index.html'
            })
            .state('menu', {
                abstract: true,
                url: '/menu',
                templateUrl: 'views/nav/menu.html'
            })
            .state('menu.main', {
                url: '/main',
                data: {
                    role: ['user', 'merchant'],
                    mode: 'online'
                },
                views: {
                    'menuContent': {
                        templateUrl: 'views/dash/dash.main.html'
                    }
                }
            })
            .
            .
            .           
        ;
    })  

And validate each view through $stateChangeStart

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    console.log(toState);
    console.log(toState.data);
});    

enter image description here

But I think the information (like role and mode) must be taken from the view and not from the $state->data. What I want is something like this:

views/dash/dash.main.html

<ion-view title="Dashboard" hide-back-button="true" myapp-role="user,merchant" myapp-mode="online" myapp-security="true">
    <ion-content has-header="true" padding="false">

    </ion-content>
</ion-view>

And be able to get myapp-role, myapp-mode, myapp-security values before the view is shown to the user. How can I achieve this? And another question, is this a good approach?

fiberOptics
  • 6,955
  • 25
  • 70
  • 105
  • I would use UI routers resolve functionality. Specify that you want the user to be resolved before the view is shown. That way the roles are available before the view is rendered. See http://www.jvandemo.com/how-to-resolve-angularjs-resources-with-ui-router/ – grimurd Sep 25 '14 at 02:13
  • can I get the custom directive values in `resolve`? – fiberOptics Sep 25 '14 at 03:09
  • If you are retrieving values from the directive then no. The directive isn't initialized until the view renders. However if possible i would rather put the data you need from the directive into a resolve so both the directive and the view/controller can use it immediately. – grimurd Sep 25 '14 at 21:50

1 Answers1

0

As I look for more references, I found out that what I want to achieve is not possible and also not a good practice.
Here I found some article that is good to follow: angular ui-router login authentication

Community
  • 1
  • 1
fiberOptics
  • 6,955
  • 25
  • 70
  • 105