我正在使用 ng-repeat 使用 jQuery 和 TB 构建手风琴。出于某种原因,这在硬编码时运行良好,但在 ng-repeat 指令内部时无法触发点击。
我在想问题出在 jQuery 没有绑定在事后加载的元素。因此,我认为与其在页面加载时加载脚本,不如在返回数据时在 .success 上加载函数。不幸的是,我无法弄清楚如何使这项工作。
测试页面:http ://staging.converge.io/test-json
控制器:
function FetchCtrl($scope, $http, $templateCache) {
$scope.method = 'GET';
$scope.url = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.web.com&key=AIzaSyA5_ykqZChHFiUEc6ztklj9z8i6V6g3rdc';
$scope.key = 'AIzaSyA5_ykqZChHFiUEc6ztklj9z8i6V6g3rdc';
$scope.strategy = 'mobile';
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $scope.url + '&strategy=' + $scope.strategy, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
$scope.updateModel = function(method, url) {
$scope.method = method;
$scope.url = url;
};
}
HTML:
<div class="panel-group" id="testAcc">
<div class="panel panel-default" ng-repeat="ruleResult in data.formattedResults.ruleResults">
<div class="panel-heading" toggle-collapse>
<h4 class="panel-title">
<a data-toggle="collapse-next" href="">
{{ruleResult.localizedRuleName}}
</a>
</h4>
</div>
<div class="panel-collapse collapse">
<div class="panel-body">
<strong>Impact score</strong>: {{ruleResult.ruleImpact*10 | number:0 | orderBy:ruleImpact}}
</div>
</div>
</div>
</div>
jQuery (在 ng-repeat 之外工作)
$('.panel-heading').on('click', function() {
var $target = $(this).next('.panel-collapse');
if ($target.hasClass('collapse'))
{
$target.collapse('show');
}else{
$target.collapse('hide');
}
});
谢谢你的帮助!