当ng-click在div中时,Ng-show不会更新

时间:2016-01-14 12:43:40

标签: html angularjs angularjs-scope angularjs-ng-click ng-show

我有一个带有ng-show的DIV。

  1. 当我运行ng-click DIV之外的元素时,它工作正常,我可以隐藏它。

  2. 当我运行ng-点击DIV内部的元素时,它不起作用。我可以看到变量beeing在我console.log时改变了,但视图不会更新。

  3. 我曾尝试使用$ scope。$ apply()但是它收到错误并说它已经在运行$ apply()。

  4. 控制器的部分:

        $scope.selectedActivity = {
            "dayNr": 0,
            "actNr": 0
        };
    
        $scope.resetSelectedActivity = function () {
            console.log("SelAct: ", $scope.selectedActivity);
            $scope.selectedActivity.dayNr = -1;
            $scope.selectedActivity.actNr = -1;
            console.log("SelAct: ", $scope.selectedActivity);
        };
    
        $scope.setSelectedActivity = function (dayNr, actNr) {
            console.log("SelAct: ", $scope.selectedActivity);
            $scope.selectedActivity.dayNr = dayNr;
            $scope.selectedActivity.actNr = actNr;
            console.log("SelAct: ", $scope.selectedActivity);
        };
    

    HTML的一部分:

        <div ng-repeat="x in xs">
                <ion-scroll>
                    <div ng-repeat="y in ys track by $index">
    
                        <div ng-click="setSelectedActivity($parent.$index, $index)">
                            <!--THE PROBLEM IS HERE-->
                            <div ng-show="selectedActivity.dayNr == $parent.$index && selectedActivity.actNr == $index">
    
                                <div>
                                    <!--THIS LOGS OUT CORRECT VALUES BUT NG-SHOW IS NOT UPDATED-->
                                    <div ng-click="resetSelectedActivity()">
                                        Reset
                                    </div>
                                </div>
    
                            </div>
                            <div>
                                <img src="img/checkButtonOverlay.png" />
                            </div>
                        </div>
                        <!--THIS LOGS OUT CORRECT VALUES AND NG-SHOW _IS_ UPDATED-->
                        <button ng-click="resetSelectedActivity()">reset</button>
                    </div>
                </ion-scroll>
            </div>
    

    请注意,由于机密性,我已从代码中删除了A LOT,但原则应该相同。

    谢谢!

1 个答案:

答案 0 :(得分:4)

发现了问题!

我有一个点击,显示外面的DIV。当我点击两次ng-clicks后输入。

首先resetSelectedActivity()然后再次在setSelectedActivity()中设置。

使用以下方法修正了它:

<div ng-click="resetSelectedActivity($parent.$index, $index, $event)">
    ...
</div>

$scope.setSelectedActivity = function (dayNr, actNr, event) {
    $scope.selectedActivity.dayNr = dayNr;
    $scope.selectedActivity.actNr = actNr;

    //This cancel the mouseclick
    event.stopPropagation();
};