最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass input value to $mdDialog - Stack Overflow

programmeradmin3浏览0评论

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 sinde the controller, it'll display normaly (see the $scope.text for example). But, if I try to get the form $scope (see `$scope.taskTitle) it just don't show anything. See my code:

JavaScript

app.controller('tasksCtrl', ['$scope', '$mdDialog',  function($scope, $mdDialog){


$scope.teste = 'Just a test, dude';

$scope.expandTask = function() {

    $mdDialog.show({
        clickOutsideToClose: true,
        controller: DialogController,
        scope: $scope,
        preserveScope: true,
        templateUrl: 'models/dialog.tmpl.php',
        locals: {
            id: $scope.tasklist.id,
            title: $scope.taskTitle
        }
    });

}

function DialogController($scope, $mdDialog, id, title) {

    $scope.id = id;
    $scope.title = title;

    $scope.hide = function() {
        $mdDialog.hide();
    };

    $scope.cancel = function() {
        $mdDialog.cancel();
      };
}
}]);

HTML

<div class="input-container float-icon" flex="100" layout="row" ng-repeat="task in tasklist">
    <md-input-container flex="100">
        <label>New Task...</label>
        <input type="text" ng-model="taskTitle" name="taskTitle">
        <md-button aria-label="Expandir Tarefa" class="md-icon-button expand-icon" ng-click="expandTask()">
            <md-tooltip hide-sm>Expand Task</md-tooltip>
            <i class="fa fa-expand"></i>
        </md-button>
    </md-input-container>

    {{taskTitle}}

</div>

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 sinde the controller, it'll display normaly (see the $scope.text for example). But, if I try to get the form $scope (see `$scope.taskTitle) it just don't show anything. See my code:

JavaScript

app.controller('tasksCtrl', ['$scope', '$mdDialog',  function($scope, $mdDialog){


$scope.teste = 'Just a test, dude';

$scope.expandTask = function() {

    $mdDialog.show({
        clickOutsideToClose: true,
        controller: DialogController,
        scope: $scope,
        preserveScope: true,
        templateUrl: 'models/dialog.tmpl.php',
        locals: {
            id: $scope.tasklist.id,
            title: $scope.taskTitle
        }
    });

}

function DialogController($scope, $mdDialog, id, title) {

    $scope.id = id;
    $scope.title = title;

    $scope.hide = function() {
        $mdDialog.hide();
    };

    $scope.cancel = function() {
        $mdDialog.cancel();
      };
}
}]);

HTML

<div class="input-container float-icon" flex="100" layout="row" ng-repeat="task in tasklist">
    <md-input-container flex="100">
        <label>New Task...</label>
        <input type="text" ng-model="taskTitle" name="taskTitle">
        <md-button aria-label="Expandir Tarefa" class="md-icon-button expand-icon" ng-click="expandTask()">
            <md-tooltip hide-sm>Expand Task</md-tooltip>
            <i class="fa fa-expand"></i>
        </md-button>
    </md-input-container>

    {{taskTitle}}

</div>
Share Improve this question edited Jul 11, 2015 at 8:58 jakeforaker 1,6571 gold badge20 silver badges34 bronze badges asked Jul 9, 2015 at 13:20 Matheus CostaMatheus Costa 251 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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>

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)

  1. 暂无评论