2015-05-04 40 views
1

开放模式我已经把里面的服务引导模式,我想从那里打开它。 但我在萤火虫中出错。该代码仍然工作正常,但有错误的萤火获取未定义的方法错误,如果从服务

我有这样

'use strict'; 

angular.module('test') 
    .factory('commonService', [ 
     '$http', 
     '$log', 
     '$modal', 
     '$q', 
     function ($http, $log, $modal, $q) { 
      var api_url = "/api/"; 
      var 
       commonService = { 

        self: this, 

        open: function (modelName, templateUrl) { 

         var modalInstance = $modal.open({ 
          templateUrl: templateUrl, 
          controller: 'ModalInstanceCtrl', 
          resolve: { 
           modelName: function() { 
            return modelName; 
           } 
          } 
         }); 

         modalInstance.result.then(function (result) { 
          console.log(result); 
         }, function() { 
          $log.info('Modal dismissed at: ' + new Date()); 
         }); 
        }// 

       }; 

      return commonService; 
     }]) 
; 

服务这是我的控制器

$scope.openModal = function (modelName) { 
    commonService.open(modelName, 'test.html').then(function (result) { 
     console.log('res') 

    }, function (error) { 
     console.log(error); 
    }) 

}; 

我喜欢这个模板中打开

ng-click="openModal('User')" 

问题是我的萤火

得到这个错误
TypeError: commonService.open(...) is undefined 


commonService.open(modelName).then(function (result) { 

但我仍然模式打开罚款与内容

回答

1

常见service.open方法应返回modalInstance.result。这里modalInstance.result是一个承诺,你可以在你的控制器中解决它。

+0

哇,这完美地工作,但我并没有完全理解这是为什么。为什么错误会这样说。什么happenning在我的情况 – user3214546

+0

ModalInstance返回基于用户交互的值,但是当用户交互,所以它返回它可以将用户后可以解决一个承诺对象与您互动modalInstance modalInstance不知道。阅读有关$ q docs.https://docs.angularjs.org/api/ng/service/$q。在你的情况下,你不会在commonservice.open中返回任何东西,所以当你调用方法common service.open()。then()时,它就像undefined.then() – Konammagari

相关问题