42


主列表頁面具有編輯按鈕。其中打開編輯行的詳細信息。
Way-1:現在,如果我設置了「ctrl.parent.q_details.client_location」,它將與父級列表控制器綁定,並且它作爲雙向綁定工具並自動更改值,如同在編輯框更改中那樣這裏沒有要求。
這裏只是我想要顯示並允許在inputbox中編輯值。不想在父控制器中更改。
將數據傳遞給mdDialog

►以下是在父控制器的代碼來調用mdDialog

$mdDialog.show({ 
       locals:{parent: $scope},     
       clickOutsideToClose: true,     
       controllerAs: 'ctrl',     
       templateUrl: 'quotation/edit/',//+edit_id, 
       controller: function() { this.parent = $scope; }, 
      }); 

►以下是彈出mdDialog的代碼。

<md-dialog aria-label=""> 
    <div ng-app="inputBasicDemo" ng-controller="deliverController" layout="column"> 
     <form name="" class="internal_note_cont">   
      <md-content class="md-padding">    
       <md-input-container class="md-input-has-value" flex> 
        <label>Client Name</label> 
        <input ng-model="qe.client_name" required > 
       </md-input-container> 
       <md-input-container flex> 
        <label>Client Location</label> 
        <input required ng-model="ctrl.parent.q_details.client_location"> 
       </md-input-container>     
      </md-content> 
     </form> 
     <div>   
     </div> 
    </div> 
    <input type="" required ng-model="ctrl.parent.q_details.recid"> 
</md-dialog> 



Way2:秒方式直接從DB發送的值而不結合NG-模型對話框控制器(deliverController)的。

]).controller("deliverController", ["$scope", "$filter","$http","$route","$window","$mdDialog", 
    function ($scope, $filter,$http,$route,$window,$mdDialog) { 
     $scope.qe.client_name = '12345'; // just to test.   
    } 

這是給取消定義$ scope.qe的錯誤。

因此,最終,我無法將數據發送到mdDialogue並顯示它們,並允許以正常方式編輯它們。 請任何人有經驗的角人幫助我。我對角度很陌生。 自2天以來,我嘗試了不同的方法。

+1

您可以使用ng-bind設置一次性綁定。您也可以通過服務在父母和孩子之間傳遞數據。 – BobDoleForPresident

+0

你嘗試過'preserveScope:true'嗎? – Ellone

回答

70

這傢伙總是有正確的答案:https://github.com/angular/material/issues/455#issuecomment-59889129

簡而言之:

$mdDialog.show({ 
      locals:{dataToPass: $scope.parentScopeData},     
      clickOutsideToClose: true,     
      controllerAs: 'ctrl',     
      templateUrl: 'quotation/edit/',//+edit_id, 
      controller: mdDialogCtrl, 
     }); 

var mdDialogCtrl = function ($scope, dataToPass) { 
    $scope.mdDialogData = dataToPass 
} 

通過使用當地人的傳球對象屬性的變量。這些值將被注入到控制器而不是$ scope。也傳遞父母的整個範圍可能不是一個好主意,因爲它打敗了孤立範圍範式。

+1

如果在mdDialogCtrl中更改了mdDialogData,更改是否會反映在$ scope.parentScopeData中?我有一個用例,它需要將當前作用域的對象傳遞給mdDialogCtrl,並且應該在外部作用域中捕獲對mdDialogCtrl中該對象的更改。謝謝! –

+1

是的,它確實反映了,如果你通過本地傳遞一個對象引用,它確實反映了,但我期望它隔離並且不直接修改父代的作用域數據,奇怪。 – Basav

+0

任何想法如何做到這一點,而不使用$範圍? –

4

HTML

<md-button ng-click='vmInter.showDialog($event,_dataToPass)'> 
<i class="fa fa-custom-edit" aria-hidden="true"></i> 
</md-button> 

的js

function _showSiebelDialog(event,_dataToPass) { 

     $mdDialog.show({ 
       locals:{dataToPass: _dataToPass}, //here where we pass our data 
       controller: _DialogController, 
       controllerAs: 'vd', 
       templateUrl: 'contentComponents/prepare/views/Dialog.tmpl.html', 
       parent: angular.element(document.body), 
       targetEvent: event, 
       clickOutsideToClose: true 

      }) 
      .then(
       function(answer) {}, 
       function() { 

       } 
      ); 
    }; 

function _DialogController($scope, $mdDialog,dataToPass) { 
console.log('>>>>>>> '+dataToPass); 
} 
1
$scope.showPrompt = function(yourObject) { 
$mdDialog.show({ 
    templateUrl: 'app/views/your-dialog.tpl.html', 
    locals: { 
     callback: $scope.yourFunction // create the function $scope.yourFunction = function (yourVariable) { 
    }, 
    controller: function ($scope, $mdDialog, callback) { 
     $scope.dialog.title = 'Your title'; 
     $scope.dialog.abort = function() { 
      $mdDialog.hide(); 
     }; 
     $scope.dialog.hide = function() { 

      if ($scope.Dialog.$valid){ 
       $mdDialog.hide(); 
       callback($scope.yourReturnValue, likes the return of input field); 
      } 
     }; 
    }, 
    controllerAs: 'dialog', 
    bindToController: true, 
    clickOutsideToClose: true, 
    escapeToClose: true 
}); 

};

0

的ES6 TL; DR方式

上飛

let showDialog = (spaceApe) => { 
    $mdDialog.show({ 
     templateUrl: 'dialog.template.html', 
     controller: $scope => $scope.spaceApe = spaceApe 
    }) 
} 

模板

瞧創建範圍變量控制器,spaceApe現在可以在對話框模板中使用

<md-dialog> 
    <md-dialog-content> 
     <span> {{spaceApe | json}} </span> 
    <md-dialog-content> 
<md-dialog>