Pass input value to $mdDialog

后端 未结 2 727
鱼传尺愫
鱼传尺愫 2021-01-15 18:32

I\'m trying to pass a form input to my dialog (as title for example). The problem is: it don\'t get the form $scope.

If I set the $scope si

相关标签:
2条回答
  • 2021-01-15 18:54

    You should pass in the task object from the ng-repeat into the ng-click.

    For ex ng-click="expandTask($event, task)"

    and in your $mdDialog controller, you will have access to that object:

    app.controller('tasksCtrl', ['$scope', '$mdDialog', function ($scope, $mdDialog) {
    
        $scope.expandTask = function (e, task) {
            //ng-click="expandTask($event, task)"
            $mdDialog.show({
                clickOutsideToClose: true,
                controller: function ($mdDialog) {
                    var vm = this;
                    vm.task = {};
                    vm.task = task;  //your task object from the ng-repeat
    
                    $scope.hide = function () {
                        $mdDialog.hide();
                    };
                    $scope.cancel = function () {
                        $mdDialog.cancel();
                    };
                },
                controllerAs: 'modal',
                templateUrl: 'models/dialog.tmpl.php',
                parent: angular.element(document.body),
                targetEvent: e
            });
        };
    }]);
    

    And in modal template you will access the task object with controllerAs notation, for example:

     <h1> {{ modal.task.name }} <h1>
    
    0 讨论(0)
  • 2021-01-15 19:06

    Keep it minimal

    and create a controller with scope variables on the fly

    $scope.expandTask = (e, task) =>
        $mdDialog.show({
            templateUrl: 'dialog.template.html',
            controller: $scope => $scope.taskName = task.name
        })
    

    taskName is now a $scope variable and can be used in the dialog template

    0 讨论(0)
提交回复
热议问题