gpt4 book ai didi

javascript - 如何自动检查嵌套 ng-repeat 中的元素?

转载 作者:行者123 更新时间:2023-11-28 06:11:01 25 4
gpt4 key购买 nike

我有一个包含 ng-repeat 元素的 div。这些 ng-repeat 元素是相互关联的。 ng-repeat 的元素有复选框。我想要的是,当检查顶部 ng-repeat 的元素时,它应该自动检查其他 ng-repeat 中的元素。下图显示了元素的实际表示,下一张图显示了我实际想要实现的目标。

enter image description here enter image description here

我确实尝试过这个,但它根本不起作用

<div actor-check-box  ng-repeat="actor in actors">

<div create-connections class="actor\{{$index}}" >
<span><input class="checkbox-actor" type="checkbox" name="actor-checkbox" id="actor-checkbox\{{$index}}">\{{actor}}</span>
</div>

<div ng-repeat="activity in activities">

<div ng-if="actor == activity.id">

<div ng-repeat = "impact in activity.text" >
<div update-connections class="impact\{{$index}}actor\{{actors.indexOf(actor)}}" actor-id="actor\{{actors.indexOf(actor)}}">
<span><input class="checkbox-activity" type="checkbox" name="impact-checkbox" id="activity-checkbox\{{$index}}actor\{{actors.indexOf(actor)}}" activity-check-box>\{{impact}}</span>
</div>

<div ng-repeat="feature in features">

<div ng-if="actor == feature.id && impact == feature.key">
<div feature-connection ng-repeat = "feature in feature.text" class="feature\{{$index}}" activity-id="impact\{{activity.text.indexOf(impact)}}actor\{{actors.indexOf(actor)}}" id="">
<span><input class="checkbox" type="checkbox" name="impact-checkbox" id="" >\{{feature}}</span>
</div>
</div>

</div>

</div>

</div>

</div>

</div>

指令代码:

    angular.module('mainModule').directive('activityCheckBox', function($interval) {
return {
restrict: 'EA',
/*replace: false,*/
scope: {
ngModel:'='
}
/*require: 'createConnections','updateConnections', 'featureConnection'*/,
/*transclude: true,*/

link: function(scope, element, attrs) {

element.find('input[type="checkbox"]').prop('checked',true);
}
};
});


angular.module('mainModule').directive('actorCheckBox', function($interval) {

return {
restrict: 'EA',
/*transclude: true,*/

link: function (scope, element, attrs, ctrl) {

console.log(element);

scope.$watch('ngModel', function(newValue){



element.find('input[type="checkbox"]').prop('activityCheckBox',true).trigger('change');

});
}
}
});

最佳答案

这无法使用我们检查和取消选中元素的正常方法来实现。我通过捕获 dom 在指令内使用了 emmit 和 brodcast。这是我编写的代码,它的工作原理非常神奇:

    angular.module('mainModule').directive('allCheckboxesBroadcast', function($interval) {
return {
restrict: 'A',
controller: function($scope) {

$scope.checkAll = function (model,id) {
if (model == true){
$scope.$broadcast('allCheckboxes',id, true);
}else{
$scope.$broadcast('allCheckboxes',id, false);
}

}

}
};
});

angular.module('mainModule').directive('allCheckboxesListeners', function($interval) {
return {
restrict: 'A',
link: function(scope, element, attrs) {

attrs.$observe('actorId', function(value) {


scope.$on('allCheckboxes', function(event, id,shouldCheckAllCheckboxes) {

actorId = 'actor' + id;
if (value == actorId){

element.find('input').prop('checked', shouldCheckAllCheckboxes);
$scope.$broadcast('featureCheckboxesListeners', actorId, true);
}

});

});

}
};
});

关于javascript - 如何自动检查嵌套 ng-repeat 中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com