0

This is what my ui-router looks like:

 .state('colleague', {
            url: "/colleague",

            templateUrl: "views/colleague.html",

            resolve: {
                typeEmployee: function ($q, $timeout) {
                    var deferred = $q.defer();
                    $timeout(function () {
                        deferred.resolve('manager');
                    }, 200);
                    return deferred.promise;
                }
                ,
                controller:  'colCtrl'
            }
        })

The issue is that I can't go to the collegue page:

<a ui-sref="colleague">colleague</a>

This is the controller code:

function colCtrl() {
    debugger;
    console.log('type of employee is:', typeEmployee);
    if (typeEmployee === 'colleague') {
        console.log('not allowed to view this');
    }

    if (typeEmployee === 'manager') {
        console.log('allowed to view this');
    }
}

app.controller('colCtrl', colCtrl);

When I grab the code from the controller and paste this directly into the router it works. What do I need to fix in the code so I can use 'controller:colCtrl' in my router?

3
  • In your routes definition, mention the ctrl as a variable, not string. ie ctrl not 'ctrl' Commented Jan 15, 2016 at 3:02
  • how st&pid am I : ( thanks for that Commented Jan 15, 2016 at 3:06
  • not st&pid all make these mistakes occasional Commented Jan 15, 2016 at 3:44

2 Answers 2

2

You are using controller inside the resolve. You should move that to top level of state config object.

        .state('colleague', {
            url: "/colleague",

            templateUrl: "views/colleague.html",

            controller:  'colCtrl', // Notice its same level as resolve

            resolve: {
                typeEmployee: function ($q, $timeout) {
                    var deferred = $q.defer();
                    $timeout(function () {
                        deferred.resolve('manager');
                    }, 200);
                    return deferred.promise;
                }

            }
        })

Here is working plunkr with your example.

Sign up to request clarification or add additional context in comments.

Comments

0

Problem is that, you need to mention the Controller as a variable, not as a string.

i.e.

controller: colCtrl

not

controller: 'colCtrl'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.