我需要在 $mdDialog 中调用一个函数。此函数正在从父级传递到我的指令。
<get-list callback="getList()" ></get-list>
在我的 get-list 指令中获取函数。
function directive() {
return {
restrict: 'E',
scope: {
callback: '&?'
},
templateUrl: "",
controller: function($scope) {
'ngInject';
}
现在在我的 get-list 指令中,我有一个 $mdDialog。
$scope.save = function(){
$mdDialog.show({
templateUrl: '',
escapeToClose: true,
clickOutsideToClose: true,
controller: function($scope) {
$scope.teste = function(){
$scope.callback()
}
}
})
}
我需要在其中调用函数 getList(),但出现错误 $scope.callback() 不是函数
最佳答案
$mgDialog
有一个与您的指令不同的隔离范围,
您可以尝试跟踪原始范围并在 $mgDialog
Controller
$scope.save = function(){
var outerScope = $scope;
$mdDialog.show({
templateUrl: '',
escapeToClose: true,
clickOutsideToClose: true,
controller: function($scope) {
$scope.teste = function(){
outerScope.callback();
}
}
})
}
或者将回调作为参数传递
$scope.save = function(){
$mdDialog.show({
templateUrl: '',
escapeToClose: true,
clickOutsideToClose: true,
locals: {
callback: $scope.callback
},
controller: function($scope, callback) {
$scope.teste = function(){
callback();
}
}
})
}
关于javascript - 从父 Controller 调用 $mdDialog 内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54257889/