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>