简体   繁体   English

Angular / Javascript-重新加载ng-repeat数据后更改类

[英]Angular/Javascript - change class after data of ng-repeat reloaded

I am using angularJs with cordova. 我正在cordova中使用angularJs。

I have a ng-repeat, on each items I have a ng-click which put the value of each item in a array. 我有一个ng-repeat,在每个项目上都有一个ng-click,它将每个项目的值放在一个数组中。 Moreover when I click, it is removing and adding a new class to the div (with the $index of ng-repeat). 而且,当我单击时,它正在删除并向div添加新类(带有ng-repeat的$ index)。 Like a checklist. 像清单。

The fact is when, I reload that ng-repeat, I lost the classes I just added when I clicked on it. 事实是,当我重新加载该ng-repeat时,当我单击它时,丢失了刚添加的类。 I tried (with the array which has not changed) to re-add the class when I call the function that reload the items shown by the ng-repeat. 当我调用重新加载ng-repeat显示的项目的函数时,我尝试(使用未更改的数组)重新添加该类。 But it doesn't add the class :/ 但是它没有添加类:/

Here are my code : 这是我的代码:

<div id="ami" class="list-group">
           <div href="#" class="list-group-item" ng-repeat="ami in listeAmis"> {{ami.pseudo}}<i id="checkAmi{{$index}}" class="fa fa-circle-o pull-right" ng-click="mapCtrl.checkAmi(ami.pseudo, $index);"></i><i class="fa fa-user pull-left" ></i></div>
</div>

Javascript Java脚本

var amisNotifies = [];

                mapCtrl.checkAmi = checkAmi;
                function checkAmi(pseudo, id) {
                    var info = ({
                        pseudo: pseudo,
                        id: id
                    });
                    var getIndexOf = function (psdu) {
                        for (var i = 0; i < amisNotifies.length; i++) {
                            if (amisNotifies[i].pseudo === psdu) {
                                return i;
                            }
                        }

                        return -1;
                    };

                    if (amisNotifies.length > 0) {
                        var index = getIndexOf(pseudo);
                        if (index > -1) {
                            //so already exists. now remove it.
                            Array.prototype.splice.call(amisNotifies, index, 1);
                            $("#checkAmi" + id).addClass("fa-circle-o");
                            $("#checkAmi" + id).removeClass("fa-check-circle-o");
                        }
                        else {
                            //does not exist, now add it
                            amisNotifies.push(info);
                            $("#checkAmi" + id).removeClass("fa-circle-o");
                            $("#checkAmi" + id).addClass("fa-check-circle-o");
                        }

                    } else {
                        amisNotifies.push(info);
                        $("#checkAmi" + id).removeClass("fa-circle-o");
                        $("#checkAmi" + id).addClass("fa-check-circle-o");
                    }
                    console.log(amisNotifies);
                }

And so, when I reload the data shown by the ng-repeat I tried to put it but it doesn't change the class again... 因此,当我重新加载ng-repeat显示的数据时,我尝试放入它,但它不会再次更改类...

if (amisNotifies.length > 0) {
            for (var i = 0; i < amisNotifies.length; i++) {
            console.log(amisNotifies[i].id);
            $("#checkAmi" + amisNotifies[i].id).removeClass("fa-circle-o");
            $("#checkAmi" + amisNotifies[i].id).addClass("fa-check-circle-o");
            }
}

HTML with dynamic ng-class stocked in array depending of the index : 具有动态ng-class的HTML存储在数组中,具体取决于索引:

<div id="ami" class="list-group">
        <div href="#" class="list-group-item" ng-repeat="ami in listeAmis"> {{ami.pseudo}}
                 <i id="checkAmi{{$index}}" ng-class="isChecked[{{$index}}]" ng-click="mapCtrl.checkAmi(ami.pseudo, $index);"></i>
        </div>
</div>

Declare the numbers of variable depending the size of ng-repeat : 根据ng-repeat的大小声明变量的数量:

if ($scope.listeAmis.length > 0) {
        for (var j = 0; j < $scope.listeAmis.length; j++) {
                 $scope.isChecked[j] = "fa fa-circle-o pull-right";
        }
}

Check the line you just clicked on and change the ng-class (moreover I stock the index of the line a just clicked in order to declare $scope.isChecked[j] differently if I clicked on it ... 检查您刚刚单击的行并更改ng-class(此外,如果我单击它,我会以不同的方式声明$ scope.isChecked [j]来存储刚刚单击的行的索引...

mapCtrl.checkAmi = checkAmi;
                function checkAmi(pseudo, id) {
                    var info = ({
                        pseudo: pseudo,
                        id: id
                    });
                    var getIndexOf = function (psdu) {
                        for (var i = 0; i < amisNotifies.length; i++) {
                            if (amisNotifies[i].pseudo === psdu) {
                                return i;
                            }
                        }

                        return -1;
                    };

                    if (amisNotifies.length > 0) {
                        var index = getIndexOf(pseudo);
                        if (index > -1) {
                            //so already exists. now remove it.
                            Array.prototype.splice.call(amisNotifies, index, 1);
                            $scope.isChecked[id] = "fa fa-circle-o pull-right";
                        } else {
                            //does not exist, now add it
                            amisNotifies.push(info);
                            $scope.isChecked[id] = "fa fa-check-circle-o pull-right";
                        }

                    } else {
                        amisNotifies.push(info);
                        $scope.isChecked[id] = "fa fa-check-circle-o pull-right";
                    }
                    console.log(amisNotifies);
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM