简体   繁体   English

传递多个参数UI Bootstrap模态窗口Angular JS无法正常工作

[英]Passing multiple parameters UI Bootstrap Modal Window Angular JS not working

I am trying to pass 4 variables into and out of a modal window, using AngularJS and UI Bootstrap. 我试图使用AngularJS和UI Bootstrap将4个变量传入和传出模态窗口。 Unfortunately when I get the parameters back from the modal window, only 1 of the parameters shows up - all the rest return as 'undefined'! 不幸的是,当我从模态窗口返回参数时,只显示其中一个参数 - 其余所有参数都返回为'undefined'!

To the code: 代码:

Here is where I open the $modal service, and pass the 4 parameters: 这是我打开$ modal服务的地方,并传递4个参数:

$scope.open = function() {

    var modalInstance = $modal.open({
        templateUrl: 'modal.html',
        controller: 'ModalController',
        resolve: {
            var1: function() { return $scope.var1; },
            var2: function() { return $scope.var2; },
            var3: function() { return $scope.var3; },
            var4: function() { return $scope.var4; }
        }
    });

These are injected to the ModalController like so: 这些注入到ModalController中,如下所示:

var ModalController = function($scope, $modalInstance, var1, var2, var3, var4) {

Inside the ModalController, I can see and use these variables just fine. 在ModalController中,我可以很好地查看和使用这些变量。

I then return them to the parent Controller by closing the modal window like this: 然后我通过关闭模态窗口将它们返回到父控制器:

var ok = function() {
    $modalInstance.close($scope.var1, $scope.var2, $scope.var3, $scope.var4);
};

The problem becomes clear when these values are received back in the parent controller - it only seems to pass the first parameter, var1. 当在父控制器中收到这些值时问题变得清晰 - 它似乎只传递第一个参数var1。 All the others are undefined! 所有其他都未定义!

modalInstance.result.then(function (var1, var2, var3, var4) {
    $scope.var1 = var1;
    $scope.var2 = var2;
    $scope.var3 = var3;
    $scope.var4 = var4;
}, function () {
    console.log("Modal dismissed at: " + new Date());
});         

Now, I see in the UI Bootstrap documentation that the "close" function expects a promise. 现在,我在UI Bootstrap文档中看到“关闭”函数需要一个承诺。 I'm pretty new to Angular and Javascript and don't really understand why 1 variable works, and not multiple? 我对Angular和Javascript很新,并不真正理解为什么1个变量有效,而不是多个? I assume I have structured things incorrectly... I don't really understand how the resolve stuff works, so I am sure I am doing something pretty stupid. 我认为我的结构不正确......我真的不明白解决方法是如何工作的,所以我确信我做的事情非常愚蠢。

Any ideas? 有任何想法吗?

return one JSON object, like: 返回一个JSON对象,如:

$modalInstance.close({'var1':$scope.var1, 'var2':$scope.var2, 'var3':$scope.var3, 'var4':$scope.var4});

and

modalInstance.result.then(function (result) {
  console.log(result);
  $scope.var1 = result.var1;
      .....
}

If you look at the signature for .then() , you'll notice that it doesn't send through an unknown number of arguments but rather very specific parameters. 如果你看一下.then()的签名,你会发现它不会通过未知数量的参数发送,而是发送非常具体的参数。 The first argument is your data. 第一个参数是您的数据。 As such, send an object representing all of your data rather than breaking it apart. 因此,发送一个表示所有数据的对象,而不是分开它。

var ok = function() {
    var data = {
        var1: $scope.var1, 
        var2: $scope.var2, 
        var3: $scope.var3, 
        var4: $scope.var4
    };
    $modalInstance.close(data);
};

modalInstance.result.then(function (data) {
    $scope.var1 = data.var1;
    $scope.var2 = data.var2;
    $scope.var3 = data.var3;
    $scope.var4 = data.var4;
}, function () {
    console.log("Modal dismissed at: " + new Date());
});   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM