如何将参数传递到 UI 路由器状态的模式窗口
How to pass parameters to a Modal window in ui-router state
我一直在开发一个 Angular 应用程序,但我被困住了。我使用 ng-repeat 遍历对象列表并可以访问此变量._id
。这是父页面的标记:当我单击删除按钮时,我想将._id
传递给父页面(当前页面)控制器,以便它可以传递到模式窗口的控制器
<tr ng-repeat="bucket in buckets">
<td>{{ bucket.name }}</td>
<td>{{ bucket._id }}</td>
<td class="col-sm-1">
<!-- View Bucket Page -->
<a ui-sref="bucketPage({ bucket_id: bucket._id })" class = "btn btn-primary">View</a>
<!-- Delete Bucket -->
<!-- <a ui-sref="userProfile({ bucket_id: bucket._id })" ng-click="deleteBucketModalWindow('sm')" class="btn btn-danger">Delete</a> -->
<a ng-click="deleteBucketModalWindow('sm')" href="#/" class="btn btn-danger">Delete</a>
</td>
</tr>
当我转到不同的页面(而不是模态)时,我得到了如何做到这一点。为此,我这样做:
<a ui-sref="userProfile({ user_id: user._id })" class = "btn btn-primary">View Profile</a>
这是父页面的控制器:
// Modal window to delete a Bucket
$scope.deleteBucketModalWindow = function(size){
var user_id = $stateParams.user_id;
var bucket_id = $stateParams.bucket_id;
console.log("bucket id in userProfileCtrl: ", user_id);
console.log("bucket id in userProfileCtrl: ", bucket_id);
var modalInstance = $modal.open({
templateUrl: 'views/deleteBucketModal.html',
controller: 'deleteBucketModalCtrl',
size: size,
bucket_id: bucket_id,
resolve: {
bucket_id: function () {
return $stateParams.bucket_id;
}
}
});
modalInstance.result.then(function(){
// Refresh the data
User.getSingleUserDetails($stateParams.user_id)
.success(function(buckets){
$scope.buckets = buckets;
})
.catch(function(response){
console.log(response);
});
}, function(){
});
};
这是模态窗口的控制器
angular.module('ResourceBucket')
.controller('deleteBucketModalCtrl', function($scope, $modalInstance, $auth, $http, $window, $rootScope, Bucket){
// Delete the bucket
$scope.deleteBucket = function(){
// close the modal
$modalInstance.close();
console.log("Inside deleteModal - bucket_id: ", $scope.bucket_id);
// bucket_id gets passed through the "resolve"
Bucket.deleteBucket($scope.bucket_id)
.then(function(data){
console.log("Just deleted a bucket!", data);
})
.catch(function(response){
console.log("In catch of deleteBucketModalCtrl: ", response);
});
};
$scope.cancel = function(){
// Just close the modal, dont do anything else
// After you close the modal, in .then() refresh the data
$modalInstance.close();
}
});
这是模态窗口的标记
<!-- Add Bucket Modal -->
<div controller="deleteBucketModalCtrl">
<div class="modal-header">
</div>
<div class="modal-body">
<ul>
<!-- Bucket Modal -->
<div class = "row">
<!-- Heading -->
<h2 class="text-center">Sure, you want to delete the bucket?</h2>
<button class="btn btn-danger" ng-click="deleteBucket()">Delete</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</ul>
</div>
<div class="modal-footer">
</div>
</div>
谢谢!
对不起,实际上我发现我做错了什么。在标记中,您可以使用 ng-click 传递参数,如下所示:
<a ng-click="deleteBucketModalWindow('sm', bucket._id)" href="" class="btn btn-danger">Delete</a>
相关文章:
- Javascript MVVM 设计模式 - 如何跟踪脏状态以及谁应该做 Ajaxing
- 如何将参数传递到 UI 路由器状态的模式窗口
- 在Redux中使用组件状态是反模式的吗
- 显示 ui 路由器模式而不刷新父状态
- 引导模式在单击时打开并保持打开状态 10 秒
- 反应.js - 处理哑儿童状态的模式
- 在 Node.js 中表示状态的普遍接受模式是什么?
- 输入模式应用程序状态,直到下次使用 Jquery 单击
- 单元测试有状态 RequireJS 模块的模式
- 用于更新Redux状态的多个部分的模式
- 基于给定状态的委派调用模式
- 无法处理请求,因为模式对话框或警报处于活动状态
- 使用单例模式维护整个应用中的数组状态
- 服务器状态维护模式标志w/ D2L
- 如何在离线模式下存储修改后的网页内容,然后再回到在线状态
- AngularUI路由器:使用相同url模式的多个状态
- 100%HTML5模式下的SoundManager卡在“停滞”状态
- 在浏览器后退时关闭模式,然后擦除转发状态
- 是否有管理事件的“量子状态变化”的有用模式?(特别是在JavaScript中)
- 在PHP中是否有可能发现客户端的计算机没有处于睡眠状态或锁定模式?