how to pass a function as a argument to another fu

2019-09-13 01:44发布

A very common question. help is really appreciated !!

i am not able to pass loginCtrl as a argument in SignupCtrl

Also if there is any proper way to do this please suggest

here is the code

     $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条回答
在下西门庆
2楼-- · 2019-09-13 02:06

Bind click event.

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;
   }

Another way of doing this,

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

its controller in the same file, its the inline controller, you can also make it in external file.

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

Second approach is, you can pass data between two controllers by use of service.

查看更多
登录 后发表回答