我想以角度触发事件点击处理程序(单击按钮并触发跨度)。我尝试使用 nth-child 选择器,但仍然没有结果。有什么建议 ?我也试过jQuery选择器......
<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
});
}
}
});
答案 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) {
任何例子?