获取控制器中的工厂返回值

时间:2015-12-04 09:00:54

标签: angularjs controller

我想在控制器端获取工厂返回值。现在我发现错误'无法读取属性'然后'未定义的'。 我打电话给工厂保存功能。' confirmmodal'是工厂。我想得到触发ok函数时返回的$ scope.test值

Controller 
$scope.save = function(){
$rootScope.showspinner = true;
if(!$scope.message) $scope.message = "Sorry no request message found..";
   // $scope.customer=confirmmodal.transientModal($scope.message);
    //console.log($scope.customer);
    confirmmodal.transientModal($scope.message).then(function(data){
    $scope.value = data;
     });     
    };

    Factory
       transientModal: function(text) {
    $rootScope.transient_modal = true;
    var modalInstance = $modal.open({
      templateUrl: 'partials/transient-modal/confirm_modeltemplate.html',
      controller: function($scope, $modalInstance) {    
                if(text) $scope.alertmsg = text; else $scope.alertmsg = "Sorry no request message found..";
                $rootScope.showspinner = true;
                $rootScope.showspinner = false;
                                    $scope.ok = function() {
                                         alert("ok button");
                                         $scope.test = 'tesating';
                                         return $scope.test;
                    //$modalInstance.dismiss('cancel');
                };  
                $scope.cancel = function() {
                    $modalInstance.dismiss('cancel');
                };
      }
    });

  }

2 个答案:

答案 0 :(得分:0)

如果你想使用.then(),你的transientModal方法应该返回一个Promise对象。

答案 1 :(得分:0)

transientModal: function(text) {
    $rootScope.transient_modal = true;
    var deferred = $q.defer();
    var modalInstance = $modal.open({
      templateUrl: 'partials/transient-modal/confirm_modeltemplate.html',
      controller: function($scope, $modalInstance) {    
                if(text) $scope.alertmsg = text; else $scope.alertmsg = "Sorry no request message found..";
                $rootScope.showspinner = true;
                $rootScope.showspinner = false;
                                    $scope.ok = function() {
                                         alert("ok button");
                                         $scope.test = 'tesating';
                                         return $scope.test;
                    //$modalInstance.dismiss('cancel');
                };  
                $scope.cancel = function() {
                    $modalInstance.dismiss('cancel');
                };
      }
    });
    modalInstance.result.then(function(test){
        deferred.resolve(test);
    },function(){
        deferred.reject();
    });
    return deferred.promise;
}

您可以试试这个,请在​​执行

之前将$ q注入工厂