ng-click在Angular JS中不起作用

时间:2016-07-27 04:51:17

标签: angularjs

当我们点击任何列表元素时,我需要更改范围值并触发onclick函数。

但是在这个代码中,当我们为list元素添加标签时,ng-click不起作用。

代码如下,

<ul class="" id="filter" path="filter">
<li value="popular" id="popular" data-faculty="Popular" class="item" onclick="SortByFilter(this, 'smartphone');" ng-click="Mobilebidirectionalsort = !Mobilebidirectionalsort"><label for="Popular">Popular</label><span class="btnSort"><input type="radio" id="Popular" name="filterOptions" class="btnRadio"></span></li>

<li value="newest" id="newest" data-faculty="High Rating" class="item" onclick="SortByFilter(this, 'smartphone');" ng-click="Mobilebidirectionalsort = !Mobilebidirectionalsort"><label for="High Rating">High Rating</label><span class="btnSort"><input type="radio" id="High Rating" name="filterOptions" class="btnRadio"></span></li>

</ul>

1 个答案:

答案 0 :(得分:0)

它应该工作正常。我已经创建了您上面描述的情况的镜像,您可以清楚地看到ng-click如何改变Mobilebidirectionalsort的价值

&#13;
&#13;
<!DOCTYPE html>
<html ng-app="myApp">

<head>
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>

<body ng-controller="myCtrl">
    <div class="col-sm-7 nopadding doc-att">
        Mobilebidirectionalsort value : {{Mobilebidirectionalsort}}
        <ul class="" id="filter" path="filter">
            <li value="popular" id="popular" data-faculty="Popular" class="item" onclick="SortByFilter(this, 'smartphone');" ng-click="Mobilebidirectionalsort = !Mobilebidirectionalsort">
                <label for="Popular">Popular</label><span class="btnSort"><input type="radio" id="Popular" name="filterOptions" class="btnRadio"></span>
            </li>
            <li value="newest" id="newest" data-faculty="High Rating" class="item" onclick="SortByFilter(this, 'smartphone');" ng-click="Mobilebidirectionalsort = !Mobilebidirectionalsort">
                <label for="High Rating">High Rating</label><span class="btnSort"><input type="radio" id="High Rating" name="filterOptions" class="btnRadio"></span>
            </li>
        </ul>

        <script>
            var myApp = angular.module("myApp", []);

            myApp.controller("myCtrl", ['$scope', function($scope) {
                $scope.Mobilebidirectionalsort = true;

            }]);


            function SortByFilter(attr1, attr2) {
                console.log('SortByFilter called');
            }
        </script>
</body>

</html>
&#13;
&#13;
&#13;