How can I trigger the click event of another element in ng-click using angularjs?

前端 未结 12 1644
耶瑟儿~
耶瑟儿~ 2020-12-03 04:05

I\'m trying to trigger the click event of the element from the button.



        
12条回答
  •  Happy的楠姐
    2020-12-03 05:06

    I just came across this problem and have written a solution for those of you who are using Angular. You can write a custom directive composed of a container, a button, and an input element with type file. With CSS you then place the input over the custom button but with opacity 0. You set the containers height and width to exactly the offset width and height of the button and the input's height and width to 100% of the container.

    the directive

    angular.module('myCoolApp')
      .directive('fileButton', function () {
        return {
          templateUrl: 'components/directives/fileButton/fileButton.html',
          restrict: 'E',
          link: function (scope, element, attributes) {
    
            var container = angular.element('.file-upload-container');
            var button = angular.element('.file-upload-button');
    
            container.css({
                position: 'relative',
                overflow: 'hidden',
                width: button.offsetWidth,
                height: button.offsetHeight
            })
    
          }
    
        };
      });
    

    a jade template if you are using jade

    div(class="file-upload-container") 
        button(class="file-upload-button") +
        input#file-upload(class="file-upload-input", type='file', onchange="doSomethingWhenFileIsSelected()")  
    

    the same template in html if you are using html

    the css

    .file-upload-button {
        margin-top: 40px;
        padding: 30px;
        border: 1px solid black;
        height: 100px;
        width: 100px;
        background: transparent;
        font-size: 66px;
        padding-top: 0px;
        border-radius: 5px;
        border: 2px solid rgb(255, 228, 0); 
        color: rgb(255, 228, 0);
    }
    .file-upload-input {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 2;
        width: 100%;
        height: 100%;
        opacity: 0;
        cursor: pointer;
    }
    

提交回复
热议问题