Javascript 离子触发器单击事件不起作用

Javascript 离子触发器单击事件不起作用,javascript,jquery,ionic-framework,ionic-view,Javascript,Jquery,Ionic Framework,Ionic View,我的Ionic应用程序中有一个模式,当显示模式时,我想触发特定元素上的点击事件 Controller $scope.$on('modal.shown', function() { document.getElementById('somemodal').click(); // #1 $( "#somemodal" ).triggerHandler( "click" ); // #2 }); Index.html <h1 class="title" id="so

我的Ionic应用程序中有一个模式,当显示模式时,我想触发特定元素上的点击事件

Controller
$scope.$on('modal.shown', function() {
      document.getElementById('somemodal').click(); // #1
      $( "#somemodal" ).triggerHandler( "click" );  // #2
});


Index.html
<h1 class="title" id="somemodal" ng-click="start()">info</h1>
控制器
$scope.$on('modal.show',function(){
document.getElementById('somemodal')。单击();/#1
$(“#somemodal”).triggerHandler(“单击”)/#2
});
Index.html
信息
#1和#2都不会触发ng click,从而触发另一个控制器中的功能启动

如有任何建议,将不胜感激

谢谢。

您应该试试这个:

<label class="item">
    <div class="button button-block button-positive" ng-click="start()">info</div>
</label>

如果两个控制器位于同一级别,请使用类似以下工厂的控制器:

(function() {
    'use strict';

    angular
        .module('yourapp')
        .factory('yourfactory', yourfactory);

    yourfactory.$inject = [$rootScope];

    /* @ngInject */
    function yourfactory($rootScope) {
        var service = {
            start: start
        };
        return service;

        function start() {
            $rootScope.$broadcast('dostart', {});
        }
     }
})();
然后在控制器1中(具有模态):

在控制器2中:

$scope.$on('dostart', function(event, args) {
    $scope.start();
});

如果两个控制器不在同一级别,也可以在没有工厂的情况下从第一个控制器直接执行
$broadcast
$emit
,具体取决于控制器的层次结构我可以通过下面的操作来执行此操作

Controller
$scope.$on('modal.shown', function() {
      $rootScope.start();
});

Another Controller
$rootScope.start= function () {
      some code ...
}

你可以使用离子模型

$ionicModal.fromTemplateUrl('yourmodelhtml.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.confirm = modal;
    });

然后调用此$scope.confirm打开单击事件。

我的建议是不要在controller@A.Wolffstart()函数位于另一个控制器中是有原因的。这就是我需要触发ng单击打开的原因。@devqon谢谢您的建议。但触发ng click以便执行该函数的方法是什么呢?也许可以使用$rootScope.$broadcast和另一个控制器中的侦听器。如果两个控制器处于同一级别,则此操作将不起作用。但在这种情况下,您可以将$broadcast添加到工厂,并从第一个控制器调用它。
Controller
$scope.$on('modal.shown', function() {
      $rootScope.start();
});

Another Controller
$rootScope.start= function () {
      some code ...
}
$ionicModal.fromTemplateUrl('yourmodelhtml.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.confirm = modal;
    });