Angularjs resolve with controller as string

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

    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;
      }]
    };
    

提交回复
热议问题