我正在尝试使用 $mdDialog 将值从 Controller 传递到服务。 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();
};
}
});
};
}
});
最佳答案
您必须将 locals
属性直接传递到 Controller 函数中,例如:
$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();
};
}
});
关于javascript - 将值传递到 $mdDialog Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42388823/