第二次通话后对话框无法正常工作

时间:2017-10-24 14:45:41

标签: angularjs

我想将数组传递给带模板的模态对话框。

由于我今天只使用AngularJS,因此99%的代码来自doc。

我的当前代码首次打开Dialog但不是之后: (md-option为空,按钮(关闭,确认)不起作用)

angular.module("materialExample").controller("calendarCtrl", function ($scope, $filter, $q, $timeout, $log, $mdDialog, MaterialCalendarData) {

$scope.showAdvanced = function(ev) {

$scope.data= ev;

$mdDialog.show({
    controller: DialogController,
    controllerAs: 'ctrl',
    templateUrl: 'includes/dialog.tmpl.html',
    parent: angular.element(document.body),
    clickOutsideToClose:true,
    fullscreen: $scope.customFullscreen ,// Only for -xs, -sm breakpoints.
    scope: $scope,
    resolve: {
        test: function() {
            return $scope.data;
        }
    }
})
    .then(function(answer) {
        $scope.status = 'You said the information was "' + answer + '".';
    }, function() {
        $scope.status = 'You cancelled the dialog.';
    });
};

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

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

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

在模板dialog.tmpl.html

<md-select ng-model="ctrl.userState">
    <md-option ng-repeat="iter in data" ng-value="iter.value.date" >
    {{iter.value.date + ' ' + iter.value.type}}
    </md-option>
</md-select>

这是传递变量的正确方法吗?如何修复代码,以便DialogController适用于连续打开的Dialogs?

删除

scope: $scope,

修复了DialogController但我无法传递数据。

1 个答案:

答案 0 :(得分:1)

可以使用 locals 属性(使用dict)调用$ mdDialog,您可以在其中提供参数:

$mdDialog.show({
controller: DialogController,
controllerAs: 'ctrl',
templateUrl: 'includes/dialog.tmpl.html',
parent: angular.element(document.body),
clickOutsideToClose:true,
fullscreen: $scope.customFullscreen ,// Only for -xs, -sm breakpoints.
scope: $scope,
**locals: {element:$scope.element}**,
resolve: {
    test: function() {
        return $scope.data;
    }
}

})

然后你的控制器:

(function() {
'use strict';

angular
    .module('myapp')
    .controller('mycontroller', mycontroller);

/* @ngInject */
function mycontroller($scope, $mdDialog, element) {
  $scope.element = element // now you can use $scope.element in this controller and it will work :)
  // what ever you need to do here
 ...
}
})()