Angular-ui bootstrap modal without creating new controller

前端 未结 3 2020
[愿得一人]
[愿得一人] 2021-02-12 14:06

plunk: http://plnkr.co/edit/85Wl5W If I use the $modalInstance on the same controller(modalController.js), without being in a modal, angular gets frozen.

I just want to

3条回答
  •  天命终不由人
    2021-02-12 14:30

    I haven't found a clean solution yet, the best workaround I found, to avoid writing the same code twice was to extend the modal controller like this:

    $.extend(this, $controller('NormalCtrl', {$scope: $scope}));
    

    full controller:

    .controller('ModalCtrl', ['$scope', '$controller', '$modalInstance', 
    function ($scope, $controller, $modalInstance) {
        //with this line here:
        // Initialize the super class and extend it.
        $.extend(this, $controller('NormalCtrl', {$scope: $scope}));
    
        // Opens a search result
        $scope.openResult = function() {
            $modalInstance.close($scope.selectedRow);
        };
    
        // Called when the cancel button is pressed
        $scope.back = function() {
            $modalInstance.dismiss('cancel');
        };
    
    }]);
    

    That way, I can re-use the same code, without having to rewrite it all over again and I can override the functions that I want to do smth different to the original controller.

    Hope I helped some people out there,

    Alex

提交回复
热议问题