how to pass a function as a argument to another function in a controller in angularjs?

混江龙づ霸主 提交于 2019-12-25 03:52:52

问题


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:


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.



来源:https://stackoverflow.com/questions/32617360/how-to-pass-a-function-as-a-argument-to-another-function-in-a-controller-in-angu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!