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
Passing data through open modal function Angular uibModal
提问by Justin Christian
I'm trying to figure out how to pass unit_number
into 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 unitNumber
with a function returning unit_number
value from it. So that you can get the unit_number
value inside AddTenantModalCtrl
by injecting unitNumber
dependency in controller factory function.
只需在解析对象中添加一个属性,unitNumber
并unit_number
从中返回值。这样你就可以通过在控制器工厂函数中注入依赖来获取unit_number
里面的值。AddTenantModalCtrl
unitNumber
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 nameunit_number
(value) and It will try to evaluate it as function. Resultant you will get$injector
error in console.
注意:不要直接做
unitNumber: unit_number
,因为当你拥有它时,angular DI 系统将尝试使用名称unit_number
(值)搜索依赖项,并尝试将其评估为函数。结果你会$injector
在控制台中得到错误。