【问题标题】:fetching factory returned value in controller在控制器中获取工厂返回值
【发布时间】:2015-12-04 17:27:45
【问题描述】:

我想在控制器端获取工厂返回值。现在我发现了错误'无法读取未定义的属性'then'。 我在保存函数中调用了工厂。'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');
                };
      }
    });

  }

【问题讨论】:

    标签: angularjs controller


    【解决方案1】:

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

    【讨论】:

    • 这种情况下如何使用promise
    【解决方案2】:
    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注入工厂

    【讨论】:

    • 这里如何查看输出?当我点击确定按钮时,我什么都看不到
    • 你可以尝试使用 $modalInstance.close($scope.test); 而不是 return 语句然后检查值是否来了?
    • 也不返回测试值
    猜你喜欢
    • 1970-01-01
    • 2017-03-12
    • 2013-07-07
    • 2016-01-25
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多