1

I am using angular-ui-router, and I have multiple ui-view divs as shown below:

<!DOCTYPE html>
<html ng-app="myApp">
    <!-- loading libraries here etc -->      
    <div ui-view="viewA"></div>    
</html>

Each view has it's own controller, and I need to resolve dependencies passed into these controllers. I have tried the following and am stumped as to why it is not working:

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider) {
  $stateProvider
    .state('index', {
      url: "",
      views: {
        "viewA": { 
            template: "{{test}}", 
            resolve: { 
                test: function(){ 
                    return {
                        value: 'Hello World!'
                    };
                } 
            }, 
            controller: testController 
        },
      }
    });
});

myApp.controller('testController', function($scope, test){
  $scope.test = test.value;
});

I have created a plunker here.

It doesn't matter whether I declare the controller before or after I call config. Putting the controller inline does work, but this would get unwieldy and difficult to maintain over time, I want to store it in it's own file.

I was under the impression that angular was smart enough to wait for dependencies to load while bootstrapping your app, is this not the case? Is the only way to put my controller inline?

Thanks

1 Answer 1

1

.config options should be written this way:

controller: 'testController'

i.e., giving the name of controller as a string. Without it, you'll just attempt to access a variable named testController - but there's none in this function's scope.

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

2 Comments

Talk about not being able to see the wood for the trees, feel like an idiot now! Thanks very much
Ah, that's fine, happens from time to time to all of us. ) Just recently answered the opposite question, where values actually should've been passed into the function in variables, not strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.