1

私は、コントローラ内からではなく、工場からuibModalを使って遊んでいました。ダイアログが表示され、OKがクリックされたときにフィールドデータがサービスに返されますが、コントローラにデータを戻す方法がわかりません。どこのモデルにポインタが追加されますか?ここで

は、私の工場のコードである:ここで

'use strict'; 

angular.module('ngTableScopeApp') 
.factory('DialogService', function($uibModal){ 

    var DialogService = {}; 
    DialogService.newObj = {}; 

    DialogService.addNewItem = function(template, $q){ 

    this.modalInstance = $uibModal.open({ 
     templateUrl: template, 
     controller: function($scope, $uibModalInstance){ 
     $scope.ok = function() { 
      $uibModalInstance.close($scope); 
      return this.newObj; 
     }; 

     $scope.cancel = function() { 
      $uibModalInstance.dismiss('cancel'); 
      return null; 
     }; 
     } 
    }); 
    }; 
    return DialogService; 
}); 

は、コントローラのコードです:

'use strict'; 

/** 
* @ngdoc function 
* @name ngTableScopeApp.controller:MainCtrl 
* @description 
* # MainCtrl 
* Controller of the ngTableScopeApp 
*/ 
angular.module('ngTableScopeApp') 
    .controller('MainCtrl', function (NgTableParams, DummyData, DialogService) { 

    var self = this; 
    self.data = DummyData.generateData(1); 

    var createUsingFullOptions = function() { 

     var initialParams = { 
     count: 10 // initial page size 
     }; 
     var initialSettings = { 
     // page size buttons (right set of buttons in demo) 
     counts: [5, 10, 25, 50], 
     // determines the pager buttons (left set of buttons in demo) 
     paginationMaxBlocks: 13, 
     paginationMinBlocks: 2, 
     dataset: self.data //DummyData.generateData(1) 
     }; 
     return new NgTableParams(initialParams, initialSettings); 
    }; 

    self.customConfigParams = createUsingFullOptions(); 

    self.addNewItem = function(){ 

     DialogService.addNewItem('views/addNewItem.html', self); 
    }; 
    }); 
+0

あなたはDを使用してコントローラからのデータにアクセスすることを願ってialogService.newObj変数。 –

答えて

1

あなたがポップアップを閉じているときにデータを渡すことができている$uibModalInstanceサービス上で利用可能close方法を、使用することができます。そして、モーダルが閉じられたときに呼び出されるresult約束オブジェクトを利用することができます。 $uibModalInstance.closeメソッドから渡されたデータは、そこから入手できます。 $uibModal.openメソッドから返された約束を返すようにしてください。

工場

DialogService.addNewItem = function(template, $q){ 

    this.modalInstance = $uibModal.open({ 
      templateUrl: template, 
      controller: function($scope, $uibModalInstance){ 
      $scope.ok = function() { 
       $uibModalInstance.close({ data: 'OK Called' }); 
      }; 

      $scope.cancel = function() { 
       $uibModalInstance.close({ data: 'Cancel Called' }); 
      }; 
      } 
     }); 
    }; 
    return this.modalInstance; 
}; 

コントローラ

DialogService.addNewItem('views/addNewItem.html', self) 
.result.then(function(data) { 
    console.log("data", data); // print { data: 'MyCustomData' } 
}); 

モーダルコントローラ

$scope.cancel = function() { 
    $uibModalInstance.close({data: 'MyCustomData'}); 
}; 
+1

は、サービスメソッド – charlietfl

+0

から '$ uibModal.open()'インスタンスを返さなければなりません。@charlietfl頭がおかげでありがとうございます。 –

+0

もまた 'modalInstance.result'を返すことができます。コントローラはちょうど約束を管理しています – charlietfl

関連する問題