如何使用相同的控制器但传递不同的参数

时间:2017-09-05 17:20:37

标签: javascript angularjs

我有两个使用相同控制器的模态,但其中一个模态不需要“用户”依赖,而另一个需要它。

将“user”作为依赖项,第一个模式显示,而第二个模式在控制台中抛出下面的错误。

ReferenceError: user is not defined

如果我从控制器中取出“user”作为依赖项,则第二个模态正常工作,但第一个模式然后抛出错误

Snippet for modal 1

如何在这个控制器上使用这两个模态?

以下代码段:

$scope.openUpdateModal = function(user) { $scope.is_edit = true; var modalInstance = $modal.open({ templateUrl: 'permissions_modal.html', controller: 'UserPermissionsCtrl', backdrop: 'static', size: 'md', resolve: { users: function () { return $scope.users; }, is_edit: function () { return $scope.is_edit; }, user: function () { return user; }, } }); };

Snippet for modal 2

$scope.openNewModal = function() { console.log("getting here") $scope.is_edit = false; var modalInstance = $modal.open({ templateUrl: 'permissions_modal.html', controller: 'UserPermissionsCtrl', backdrop: 'static', size: 'md', resolve: { users: function () { return $scope.users; }, is_edit: function () { return $scope.is_edit; }, } }); modalInstance.result.then(function (user) { $scope.users.push($scope.user); }, function () { // modal closed }); };

Snippet for controller

angular.module('app').controller('UserPermissionsCtrl', function ($scope, $modalInstance, CSRF, Permissions, users, is_edit, user) { $scope.loading = true; $scope.users = users; $scope.currentItemTab = 1; $scope.is_edit = is_edit; $scope.permission = { permission: '', options_as_json: '' }; $scope.errors = { permissions: {} }; if($scope.is_edit){ $scope.original_user = user; $scope.user = angular.copy(user); $scope.loading = false; $scope.user.permissions = []; Permissions.getUserPermissions($scope.user.username) .then(function(permissions) { _.forEach(permissions, function(p) { if (p.options) { p.options_as_json = JSON.stringify(p['options']); } }); $scope.user.permissions = permissions; }); } else { $scope.user = { username: '', }; $scope.loading = false; $scope.user.permissions = []; $scope.user.roles = []; } }

show()

0 个答案:

没有答案