0

I want to trigger an event click handler in angular (click the button and trigger also the span). I tried to use nth-child selector but still no results. Any suggestions ? I tried also with jQuery selector ...

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 Answers 2

1

Try with this controller :)

   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
                });
            }
        }
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

I have it top : $scope.forceClick = forceClick;
1

try to inject $scope in the controller

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

Any examples?

1 Comment

that is not valid, you define the controller in 2 most-used ways, .controller('name',['$scope',function($scope){ //code }]) with the purpose of resolving minification conflicts, or .controller('name',ctrlName); ctrlName.$inject = ['$scope']; function ctrlName($scope){ //code } ; If the "$scope" hasn't been injected, my binding wouldn't bind / work also :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.