[英]AngularJS: Which is better way to pass parameter to Modal Controller?
I have seen two ways of passing the parameters to the AngularJS
Modal
, one via resolve
binding and another via scope
binding. 我已经看到了将参数传递给AngularJS
Modal
两种方法,一种是通过resolve
绑定,另一种是通过scope
绑定。 I was wondering if one is better than the other and why? 我想知道一个是否比另一个更好,为什么?
Resolve Binding 解决绑定
$modal.open({
templateUrl: 'partials.html',
controller: 'MyCtrl',
resolve: {
someData: function(){
return 'Some Data';
}
}
})
.controller('MyCtrl',function(someData){
console.log(someData); // prints Some Data
})
Scope Binding 范围绑定
var scope = $rootScope.$new();
scope.someData = 'Some Data';
$modal.open({
templateUrl: 'partials.html',
controller: 'MyCtrl',
scope: scope
})
.controller('MyCtrl',function($scope){
console.log($scope.someData); // prints Some Data
})
Resolve is not considered as binding in this case. 在这种情况下, 解析不被视为具有约束力 。 When $modal.open
it pass the value to the controller through resolve. 当$modal.open
它将值通过resolve传递给控制器。 Which means if you change the value in the parent controller, modal won't update unless it's re-initialize. 这意味着,如果您在父控制器中更改值,则除非重新初始化,否则模态不会更新。 However, You can pass in promises in resolve, which means if you are waiting for data coming back from server, using resolve can prevent modal loads before data come back. 但是,您可以在solve中传递promise,这意味着如果您正在等待数据从服务器返回,则使用resolve可以防止在数据返回之前进行模式加载。
resolve: {
someData: function() {
return $http.get('someurl');
}
}
Pros & Cons 优点缺点
- Run code asynchronously 异步运行代码
- Slower (if use promise) 较慢(如果使用承诺)
- Does not bind with parent scope value 不与父范围值绑定
Scope binding will allow you to have one-way binding data, whenever the data in the parent scope update, the value in the modal can update it's value simultaneously. 范围绑定将使您具有单向绑定数据,只要父范围中的数据更新,模态中的值就可以同时更新其值。
Pros & Cons 优点缺点
- Faster 快点
- Binding with parent scope value 与父范围值绑定
- Might have scope pollution if you alter value in modal controller 如果更改模式控制器中的值,可能会造成示波器污染
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.