Javascript 通过开放模态函数Angular uibModal传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38311159/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:18:38  来源:igfitidea点击:

Passing data through open modal function Angular uibModal

javascriptangularjsangularjs-scopemodal-dialogangular-ui-bootstrap

提问by Justin Christian

I'm trying to figure out how to pass unit_numberinto the modal when it pops up. I'm pretty new with Angular and I'm a little confused with what resolve:and group:are doing and how I can include the unit_number in that return statement.

我试图弄清楚如何unit_number在弹出时进入模态。我对 Angular 很陌生,我对什么resolve:group:正在做什么以及如何在该 return 语句中包含 unit_number感到有些困惑。

    $scope.openTenantModal = function (unit_number) {
  var modalInstance = $uibModal.open({
    animation: true,
    templateUrl: 'views/addtenantmodal.html',
    controller: 'AddTenantModalCtrl',
    size: 'large',
    resolve: {
      group: function () {
        return $scope.group;
      }
    }
  });
  modalInstance.result.then(function () {
  }, function () {
  });
};

回答by WalksAway

You are using ui-bootstrap

您正在使用 ui-bootstrap

Bootstrap components written in pure AngularJS

用纯 AngularJS 编写的 Bootstrap 组件

To pass a variable to a modal's controller you need to use

要将变量传递给模态控制器,您需要使用

resolve: {
   A: function() {
       return 'myVal'
   }
}

And then you can access that variable 'A' from the modal`s controller by injecting it

然后您可以通过注入它从模态控制器访问该变量“A”

controller: ['A', function(A) {
    // now we can add the value to the scope and use it as we please...
    $scope.myVal = A;
}]

Check out: https://angular-ui.github.io/bootstrap/#/modal

查看:https: //angular-ui.github.io/bootstrap/#/modal

Resolve:

解决:

Members that will be resolved and passed to the controller as locals; it is equivalent of the resolve property in the router.

将被解析并作为本地人传递给控制器​​的成员;它相当于路由器中的解析属性。

And group is just a member (it could be anything you choose)

和组只是一个成员(它可以是你选择的任何东西)

回答by Pankaj Parkar

Just add a property in resolve object unitNumberwith a function returning unit_numbervalue from it. So that you can get the unit_numbervalue inside AddTenantModalCtrlby injecting unitNumberdependency in controller factory function.

只需在解析对象中添加一个属性,unitNumberunit_number从中返回值。这样你就可以通过在控制器工厂函数中注入依赖来获取unit_number里面的值。AddTenantModalCtrlunitNumber

resolve: {
  group: function () {
    return $scope.group;
  },
  unitNumber: function(){
    return unit_number
  }
}

Note: Don't directly do unitNumber: unit_number, because when you have that, angular DI system will try to search the dependency with name unit_number(value) and It will try to evaluate it as function. Resultant you will get $injectorerror in console.

注意:不要直接做unitNumber: unit_number,因为当你拥有它时,angular DI 系统将尝试使用名称unit_number(值)搜索依赖项,并尝试将其评估为函数。结果你会$injector在控制台中得到错误。