我有多个按钮由ng-repeat
生成。我正试图将每个按钮连接到alertify.js
确认功能。我认为最好的方法是用$index
动态生成每个按钮的ID,然后通过控制器访问$index
。我认为如果$index
实际上通过了,至少有一个按钮会起作用,但它们都不起作用。我没有要求任何人发布奇迹代码。我只想知道我使用的方法是否合理,如果不合理,有什么替代方法?
控制器逻辑
function JsonController($scope, $http) {
$scope.activeOn = true;
$scope.activeOff = false;
$http.get('flat.json')
.then(function(res) {
$scope.flats = res.data;
});
/**
* ALERTIFY LOGIC
*/
$scope.confirmButton = function(index) {
$('confirm-' + index).click(function(){
alertify.confirm("Are you sure?",
function(){
alertify.success('Yes');
},
function(){
alertify.error('No');
});
});
}
}
<<p> HTML按钮/strong> <button id="confirm-{{$index}}" class="btn btn-primary" ng-click="confirmButton($index)">Rebuild</button>
你的jquery选择器中缺少一个#。
应该是$('#confirm-' + index)
来选择id。