我正在尝试使用$ 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();
};
}
});
};
}
});
答案 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();
};
}
});