Pass input value to $mdDialog

后端 未结 2 728
鱼传尺愫
鱼传尺愫 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:

     

    {{ modal.task.name }}

提交回复
热议问题