javascript - 将输入值传递给 $mdDialog

标签 javascript angularjs angular-material

我正在尝试将表单输入传递到我的对话框(例如作为标题)。问题是:它没有得到 $scope 的形式。

如果我设置 $scope sinde Controller ,它将正常显示(例如,参见 $scope.text)。但是,如果我尝试获取表单 $scope(请参阅 `$scope.taskTitle),它不会显示任何内容。查看我的代码:

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>

最佳答案

您应该将 task 对象从 ng-repeat 传递到 ng-click。

例如 ng-click="expandTask($event, task)"

在您的 $mdDialog Controller 中,您将有权访问该对象:

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
        });
    };
}]);

并且在模式模板中,您将使用 controllerAs 符号访问任务对象,例如:

 <h1> {{ modal.task.name }} <h1>

关于javascript - 将输入值传递给 $mdDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318748/

相关文章:

javascript - jQuery Lightbox 不能与 mod_rewrite 一起使用吗?

javascript - 可以在不重新加载框架的情况下调整 iframe 的大小吗?

angularjs - 没有 promise 的 Angular anchor 卷轴

javascript - Node 智能卡在扫描卡时抛出 SCardConnect 错误,断开连接后抛出 SCardListReaders 错误

angular - 如何在同一组件上应用多个 MAT_DATE_FORMATS

javascript - 单击了哪个提交按钮

javascript 优化和缩小 vs. gzipping

javascript - Angular + jQuery 产生 "Uncaught object"错误而不是显示它

angularjs - 带有 angularjs 的 Material 设计 md-tabs

html - Angular Material Nav Sidebar 仅在响应式调整大小时显示