Reputation: 2325
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 ...
<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
});
}
}
});
Upvotes: 0
Views: 386
Reputation: 71
try to inject $scope
in the controller
.controller('MainCtrl', '$scope', function($scope, $timeout) {
Any examples?
Upvotes: 1
Reputation: 2743
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
});
}
}
}
);
Upvotes: 1