Angularjs resolve with controller as string

前端 未结 4 1794
醉梦人生
醉梦人生 2020-12-08 15:02

My style of writing angular controllers is like this (using controller name instead of function)

angular.module(\'mymodule\', [
])
    .controller(\'myContro         


        
4条回答
  •  孤城傲影
    2020-12-08 15:51

    The controller definition and resolve parts are to be specified separately on the route definition.

    If you define controllers on a module level you need to reference them as string, so:

     $routeProvider.when('/someroute', {
            templateUrl: 'partials/someroute.html', 
            controller: 'myController',
            resolve: {
              myVar: function(){
                //code to be executed before route change goes here
              };
            });
    

    The above code also shows how to define a set of variables that will be resolved before route changes. When resolved those variables can be injected to a controller so taking the example from the snippet above you would write your controller like so:

    .controller('myController', ['$scope', 'myVar', function($scope, myVar) {
                // myVar is already resolved and injected here
            }
        ]);
    

    This video might help as well: http://www.youtube.com/watch?v=P6KITGRQujQ

提交回复
热议问题