如何创建一个selectAll复选框,并在每个复选框上触发ng-click的每个函数



我有一些代码,当你点击一个复选框,它将执行ng-click。以下是它的JS。

$scope.selectTitle = function(evt, selected){
        evt.stopPropagation();
        var filtered = _.findWhere($scope.selectedTitles, {id: selected.id});
        var index = _.indexOf($scope.selectedTitles, selected);
        if(selected === filtered){
            $scope.selectedTitles.splice(index, 1)
        }
        else{
            $scope.selectedTitles.push(selected);
        }
        console.log('titles', $scope.selectedTitles, 'filtered', filtered, 'index', index);
     };

它在一个带有ng-repeat和ng-click代码的表中,因此我使用.stopPropagation()来防止激活表的ng-click功能。

现在我需要创建一个全选复选框。这是我的代码。

$scope.selectAll = function (filteredTitles) {
        if ($scope.selectedAll) {
            $scope.selectedAll = false;
        } else {
            $scope.selectedAll = true;
        }
        _.forEach(filteredTitles, function(cpPortfolioItem) {
            cpPortfolioItem.Selected = $scope.selectedAll;
            if(cpPortfolioItem.Selected){
                $scope.selectTitle();
            }
     });

当我运行它时。有一个错误说TypeError: Cannot read property 'stopPropagation' of undefined

我不能删除stopPropagation,因为它阻止了我前面所说的。你能给我一些建议,我怎样才能选中所有的复选框,并调用每个复选框的ng-click函数?

几点思考

为什么用selectedAll添加作用域?应该是控制器内部的私有变量。

var selectedAll = false;
$scope.selectAll = function(){
      selectedAll = !selectedAll; // quick toggle.
      _.forEach(filteredTitles, function(title){
                    title.isSelected = selectedAll;
       }
}

那么你的复选框应该直接钩到标题上。isSelected状态。单独更改或使用selectAll.

将非常容易。

见:https://docs.angularjs.org/api/ng/directive/ngChecked

<input ng-checked="title.isSelected" .../>

其中'title'实际上是你的ng-repeat数据对象。

我还建议在你的ng-repeat中使用一个指令。

示例指令:

angular.module('appName')
    .directive('portfolioItem', function() {
        return{
            restrict:'E',  // Element only style
            replace:true,
            templateUrl:'portfolioItem.view.html',
            scope:{
                data:'='  // binds the attribute into your scope
            }
            // You can add a controller here as well. 
        };
    });

然后为"portfolioItem.view.html"创建一个脚本ng-template

<script ng-template="portfolioItem.view.html">
    <section class="item">
     {{data}}  
     <input ng-checked="data.isSelected" ... />
    </section>
</script>
https://docs.angularjs.org/api/ng/directive/script

如果我能再帮你一点忙的话。我认为你的选择项目功能应该改变。将您的数据推入工厂,然后它可以成为您跨所有控制器的主干。这就是我们所做的,减少你的观察者,提高你处理数据的能力。
 angular.module('appName')
        .factory('DataManager', DataManager);
    function DataManager($log, $timeout, ItemModel) {
        var mngr, config = getConfig();  // any default values
        $log.debug('DataManager Init');
        mngr = {
            CurrentSearchTerm: null,
            Items: [],
            Abort: abort,
            GetData: getData,  // Function call to get data.
            GetMoreResults: getMoreResults
        };
        function getData(){
            dataService.getData().then(function(response){
             //  ...parse data, etc...loop and :
             mngr.Items.push(parsedDataItem);
            };
        }

        return mngr;
   }

然后您的控制器将重复关闭DataManager。项(或过滤器,它将下划线或Angular)。有意义吗?

这不是最漂亮的解决方案,但是,这做了两件事,只处理evt,如果定义,并将元素从selectAll传递到selectTitle。这是对代码更改最少的解决方案。一般来说,你应该能够摆脱没有selectTitle,只是使用ng-model在你的复选框。

$scope.selectTitle = function(evt, selected){
    if(evt) evt.stopPropagation();
    var filtered = _.findWhere($scope.selectedTitles, {id: selected.id});
    var index = _.indexOf($scope.selectedTitles, selected);
    if(selected === filtered){
        $scope.selectedTitles.splice(index, 1)
    }
    else{
        $scope.selectedTitles.push(selected);
    }
    console.log('titles', $scope.selectedTitles, 'filtered', filtered, 'index', index);
 };

,

$scope.selectAll = function (filteredTitles) {
        if ($scope.selectedAll) {
            $scope.selectedAll = false;
        } else {
            $scope.selectedAll = true;
        }
        _.forEach(filteredTitles, function(cpPortfolioItem) {
            cpPortfolioItem.Selected = $scope.selectedAll;
            if(cpPortfolioItem.Selected){
                $scope.selectTitle(null, cpPortfolioItem);
            }
     })};

最新更新