Angularjs resolve with controller as string

前端 未结 4 1783
醉梦人生
醉梦人生 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:41

    @TruongSinh answer worked for me and is way nicer than having additional functions in the router. I tweaked it a little as it was returning the deferred object instead of the actual resolved data.

    $routeProvider.when('/someroute', {
        templateUrl: 'partials/someroute.html', 
        controller: 'SomeController',
        resolve: {
           myModel: 'myModel'
        }
    });
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-08 15:54

    @pkozlowski.opensource 's answer works, but I don't really want to mess up my routing and and controllers, because I always keep it separated (from Yo Generator). Actually, we can also have controller and resolve(r) all as string/name (NOT function).

    angular.module('mymodule', [
    ])
      .controller('myController', [
          '$scope', 'myModelCombination'
          function($scope, myModelCombination) {
              // myModelCombination[0] === (resolved) myModel 
              // myModelCombination[1] === (resolved) myModel2
    
          }
      ])
      .controller('myController2', [
          '$scope', 'myModel'
          function($scope, myModel) {
              // Some code here
    
          }
      ])
      .factory('myModel', [
          '$scope',
          function($scope) {
              // return a promise
    
          }
      ])
      .factory('myModel2', [
          '$scope',
          function($scope) {
              // return a promise
    
          }
      ])
      .factory('myModelCombination', [
          '$scope', 'myModel', 'myModel2'
          function($scope) {
              return $q.all(['myModel', 'myModel2']);
    
          }
      ]);
    

    Then in your routing file this should be added

    $routeProvider.when('/someroute', {
        templateUrl: 'partials/someroute.html', 
        resolve: ['myModel'] //ALWAYS IN ARRAY)
    });
    $routeProvider.when('/myModelCombination', {
        templateUrl: 'partials/someroute2.html', 
        resolve: ['myModel'] //ALWAYS IN ARRAY)
    });
    

    http://docs.angularjs.org/api/ng.$routeProvider

    0 讨论(0)
  • 2020-12-08 15:54

    This would work too

    var MyController = myApp.controller('MyController', ['$scope', 'myData', function($scope, myData) {
      // Some code here
    }]);
    
    MyController.resolve = {
      myData: ['$http', '$q', function($http, $q) {
        var defer = $q.defer();
    
        $http.get('/foo/bar')
          .success(function(data) {
            defer.resolve(data);
          })
          .error(function(error, status) {
            defer.reject(error);
          });
    
        return defer.promise;
      }]
    };
    
    0 讨论(0)
提交回复
热议问题