Angularjs resolve with controller as string

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

提交回复
热议问题