如何在角度js中触发ng-repeat中的单击?

时间:2016-06-03 13:35:36

标签: javascript jquery angularjs

我想以角度触发事件点击处理程序(单击按钮并触发跨度)。我尝试使用 nth-child 选择器,但仍然没有结果。有什么建议 ?我也试过jQuery选择器......

JsFiddle

<div ng-app="app" ng-controller="MainCtrl">
  <h3 ng-bind="version"></h3>
  <div id="wrapper">
   <ul>
    <li ng-repeat="item in items">
      <button ng-click="forceClick(item,$index)">Click</button>
      <span ng-bind='item.name' ng-click='showMsg(item.name)'></span>
    </li>
   </ul>
 </div>
</div>


angular.module('app',[])
.controller('MainCtrl', function($scope,$timeout){
$scope.version = 'Angular 1.4.8';
$scope.items = [];
$scope.showMsg = showMsg;
$scope.forceClick = forceClick;

init();

function forceClick(item, index){
 $timeout(function(){
    angular.element('#wrapper ul li:eq(' + index + ')  span').triggerHandler('click');
 },3000);
}

function showMsg(itemName){
  alert("Clicked on " + itemName);
};

function init(){
 for(var i=0;i<10;i++){
    $scope.items.push({
    name:'item ' + i,
    selected:false
  });
  }
 }
});

2 个答案:

答案 0 :(得分:1)

尝试使用此控制器:)

   angular.module('app', []).controller('MainCtrl', function($scope, $timeout) {
        $scope.version = 'Angular 1.4.8';
        $scope.items = [];
        $scope.showMsg = showMsg;
        $scope.forceClick = forceClick;

        init();

        $scope.showMsg = function(itemName) {
            alert("Clicked on " + itemName);
        };
        $scope.forceClick = function(item, index) {
            console.log('I clicked !!');
        };

        function init() {
            for (var i = 0; i < 10; i++) {
                $scope.items.push({
                    name:'item ' + i,
                    selected:false
                });
            }
        }
    }
);

答案 1 :(得分:1)

尝试在控制器中注入$scope

.controller('MainCtrl', '$scope', function($scope, $timeout) {

任何例子?