将值传递给$ mdDialog控制器

时间:2017-02-22 10:37:57

标签: javascript angularjs

我正在尝试使用$ mdDialog将控制器中的值传递给服务。 var id具有值,但是当我转到testService时,该值不存在。

module.controller('viewTestController', function ($scope, $routeParams, $mdDialog, testService) {
var self = this;
self.$onInit = function () {
    self.test = function () {
        var id = $scope.$parent.key;
        console.log(id);

        $mdDialog.show(
             locals: { dataToPass: id },
             templateUrl: '/test.html',
             title: "Test",
             clickOutsideToClose: true,
             scope: $scope,
             preserveScope: true,
             controller: function ($scope) {

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

1 个答案:

答案 0 :(得分:3)

您必须将locals属性直接传递到控制器功能中,如:

$mdDialog.show(
    locals: { dataToPass: id },
    templateUrl: '/test.html',
    title: "Test",
    clickOutsideToClose: true,
    scope: $scope,
    preserveScope: true,
    controller: function ($scope, $mdDialog, dataToPass) {
        $scope.finalizeTest = function () {
            testService.finalizeTest(dataToPass);
            $mdDialog.cancel();
        };
    }
});