用于触发点击事件的angular.element不起作用

时间:2015-11-28 11:54:52

标签: angularjs

我一直在尝试使用其他元素中的角度ng-click在我的input元素中触发click事件,如下所示:

HTML

<div class="myClass" ng-click="vm.selectImage()" nv-file-drop uploader="vm.uploadImage">
    Drop your image here
</div>
<div ng-hide="!hideInput">
    <input type="file" id="imgSelect" nv-file-selec uploader="vm.uploadImage" />
</div>

控制器

vm.selectImage= selectImage;
function selectImage() {
    angular.element('#imgSelect').trigger('click');
};

我知道还有其他类似的问题,但我已经尝试使用他们所说的内容(其中一些显示了我正在使用的相同代码),例如:

但即便如此,甚至使用这样的指令:

.directive('selectImg', selectImg);
function selectImg() {
    return {
        restrict: 'A',
        link: function(scope, element) {
            element.bind('click', function(e) {
                //option 1
                angular.element(e.target).siblings('.imgSelect').trigger('click');

                //option 2
                angular.element( document.querySelector( '#imgSelect' ) ).trigger('click');

                //option 3
                var myEl = angular.element( document.querySelector( '#imgSelect' ) );
                myEl.trigger('click');

                //option 4
                angular.element('#imgSelect').trigger('click'); //angular way
            });
        }
    };
}; 

我一直收到这个错误:

  

错误:[jqLit​​e:nosel] http://errors.angularjs.org/1.5.0-beta.1/jqLite/nosel

这是一个演示错误的plunker:http://plnkr.co/edit/rWcCbixwFArYhCxUVTsv?p=preview

有什么问题?

1 个答案:

答案 0 :(得分:5)

确定更新了我的答案。这看起来你必须打破当前的$ apply()循环。一种方法是使用$ timeout()

Exception in thread "main" java.lang.ExceptionInInitializerError
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.<clinit>(EntityManagerFactoryProvider.java:55)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:92)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at Main.main(Main.java:13)
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.platform.server.NoServerPlatformDetector.checkPlatform(NoServerPlatformDetector.java:28)
at org.eclipse.persistence.platform.server.ServerPlatformUtils.detectServerPlatform(ServerPlatformUtils.java:58)
at org.eclipse.persistence.internal.jpa.IsolatedHashMap.<clinit>(IsolatedHashMap.java:48)
... 6 more

检查回答Trigger input file click event in AngularJS