jQuery Angular UI Bootstrap $scope.$modalInstance.close 原因:无法读取未定义的属性“值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21768778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular UI Bootstrap $scope.$modalInstance.close causes: Cannot read property 'value' of undefined
提问by Baconbeastnz
I have a few modals, 2 are working perfectly, 1 is getting this exception when closing. (It does manage to close the modal, but angular logs out this exception).
我有几个模态,2 个工作正常,1 个在关闭时收到此异常。(它确实设法关闭了模态,但 angular 会注销此异常)。
I've looked closer and, the $modalInstance
is defined inside the close
method, but openedWindows.get($modalInstance)
returns undefined
.
我仔细看了看,$modalInstance
是在close
方法内部定义的,但openedWindows.get($modalInstance)
返回undefined
.
How can I fix this?
我怎样才能解决这个问题?
回答by Narayana
This is a bug in $modal in v0.10.0. See this github issue, and will be fixed in the next release.
这是 v0.10.0 中 $modal 的一个错误。请参阅此 github 问题,并将在下一个版本中修复。
@IvanZhhas provided answer for a similar question here - Dismiss angular modal on URL change - errors in console
@IvanZh在这里提供了类似问题的答案 -关闭 URL 更改时的角度模式 - 控制台中的错误
In you controller, once you do $modal.open, add a finally block where you explicitly set the modalInstance to null. plunkr for dismiss modal on URL change is available in the above question. Yours should be very similar.
在您的控制器中,一旦您执行 $modal.open,添加一个 finally 块,您将 modalInstance 显式设置为 null。plunkr 用于在 URL 更改时关闭模式在上述问题中可用。你的应该非常相似。
$scope.modalInstance = $modal.open({
templateUrl: 'add.html',
controller: 'AddCtrl'
});
$scope.modalInstance.result.then(function() {
console.log('Success');
}, function() {
console.log('Cancelled');
})['finally'](function(){
$scope.modalInstance = undefined // <--- This fixes
});
回答by user745852
You can also get this error if you try to close the $modal immediately. I ran into this when I accidentally forgot to include a delay argument in my call to $timeout. Here is the bad code:
如果您尝试立即关闭 $modal,您也会收到此错误。当我不小心忘记在调用 $timeout 时包含延迟参数时,我遇到了这个问题。这是错误的代码:
var modal = $modal.open({
templateUrl: 'static/partials/simple.dialog.html',
controller: function($scope) {
$scope.opts = opts;
}
});
$timeout(function() {
simple.close();
});
This was fixed by adding the timeout:
这是通过添加超时来解决的:
$timeout(function() {
simple.close();
}, opts.timeout);
回答by Damian Green
$The documentation (https://angular-ui.github.io/bootstrap/) says : In addition the scope associated with modal's content is augmented with 2 methods:
$The documentation ( https://angular-ui.github.io/bootstrap/) 说:此外,与模态内容相关的范围增加了两种方法:
$close(result)
$关闭(结果)
$dismiss(reason).
$驳回(原因)。
The javascript though doesn't tally with this and injecting a $modalInstance doesn't work. This worked for me:
javascript 虽然不符合这一点,并且注入 $modalInstance 不起作用。这对我有用:
app.controller('myController', [ '$scope',
function ( $scope) {
..
$scope.addClient = function () {
$scope.$close(); // or $scope.$dismiss();
}
}
回答by Parker Heindl
This method didn't work well for me.
这种方法对我来说效果不佳。
I too had 'value' undefined messages, from a series of dialogs based on the state of application. One dialog was a spinning icon "Your request is Processing", that was closed in the parent scope once an $http request resolved.
我也有“价值”未定义的消息,来自基于应用程序状态的一系列对话框。一个对话框是一个旋转图标“您的请求正在处理”,一旦 $http 请求解决,它就会在父作用域中关闭。
But it wouldn't close using this and the aforementioned result.then:
但是它不会使用这个和上述结果关闭。那么:
if ($scope.modalInstance) {
$scope.modalInstance.dismiss();
}
...
$scope.modalInstance.result.then(function() {
console.log('Success');
}, function() {
console.log('Cancelled');
})['finally'](function(){
$scope.modalInstance = undefined;
});
Everything began to work when I switched to:
当我切换到:
$scope.modalInstance.result.then(function() {
...
$scope.modalInstance = undefined;
}, function() {
...
$scope.modalInstance = undefined;
});
Maybe the error was that $scope.modalInstance was set to undefined without ever closing/dismissing the original instance.
也许错误是 $scope.modalInstance 被设置为 undefined 而没有关闭/关闭原始实例。