如何将函数作为参数传递给angularjs中控制器中的另一个函数?

时间:2015-09-16 19:44:39

标签: angularjs angular-ui-bootstrap

一个非常常见的问题。非常感谢帮助!!

我无法将loginCtrl作为参数传递给SignupCtrl

如果有任何正确的方法,请建议

这是代码

     $scope.loginCtrl = function ($scope, $modalInstance) {
            $scope.cancelLogin = function () {
                $modalInstance.dismiss('cancel');
            }
        };

     $scope.signupCtrl = function ($scope, $modalInstance,loginCtrl){
$scope.close1=loginCtrl.cancelLogin;
            $scope.cancelLogin = function () {
                $modalInstance.dismiss('cancel');
            }
        };

1 个答案:

答案 0 :(得分:1)

绑定点击事件。

user.showModalDialog = function (item) {

    var obj = {
        selectedItem: item
    };
    _showModalDialog(obj);

};

_showModalDialog Method

var _showModalDialog = function (params) {
    $modal.open({
        templateUrl: "your html template URL",
        backdrop: 'static',
        windowClass: 'modal-width-50',
        resolve: {
            params: function () { return params; }
        },
        controller: function ($scope, $modalInstance, params) {
           var user = params.selectedItem;
          // you can receive your params here in params.
          $scope.user=user;
   }

另一种方法,

var _showModalDialog = function (params) {
        $modal.open({
            templateUrl: "your html template URL",
            backdrop: 'static',
            controller: _userCtrl,
            windowClass: 'modal-width-50',
            resolve: {
                params: function () { return params; }
            }});

它的控制器在同一个文件中,它是内联控制器,你也可以把它放在外部文件中。

var _userCtrl = ['$scope', '$modalInstance', 'params', function ($scope, $modalInstance, params) {
       var user = params.selectedItem;
       // you can receive your params here in params.
 $scope.user = user;
    }];

第二种方法是,您可以使用服务在两个控制器之间传递数据。