javascript - 通过 ng-click 更改颜色

标签 javascript jquery angularjs

我想在单击第 th 时更改单元格的颜色,但是当我单击时,我的颜色不保留。

我希望当我第二次点击另一个按钮时,我的第一个th不要保留颜色

HTML:

    ....



 <script type="text/ng-template" id="custom-footer" >
        <div>
            <table cellspacing="0" cellpadding="0" ng-controller="SearchController"
                   style="border:solid 1px black; font-family:verdana; font-size:12px;">
                <thead>
                <tr style="background-color:lightgrey;">
                    <th style="width:20px;" ng-class="{'selected'}" ng-click="changeTime('0')">Journée</th>
                    <th style="width:20px;" ng-class="{'selected'}" ng-click="changeTime('1')">Matin</th>
                    <th style="width:20px;" ng-class="{'selected'}" ng-click="changeTime('2')">Midi</th>
                    <th style="width:20px;" ng-class="{'selected'}" ng-click="changeTime('3')">Soir</th>
                </tr>
                </thead>
                {{test}}
            </table>
        </div>
    </script>
    <div class="ui-datepicker-calendar columns small-3">
        <input type="text" ng-model="goDate" ng-click="updateDatePicker()"
               placeholder="Date d'aller" datepicker/>
    </div>

Controller :

angular.module('matrixarSearch', [
    'mm.foundation',
    'matrixarAutocomplete',
    'matrixarCalendar'
]).controller('SearchController', function ($scope, $translate) {

    var init = function(){
        $scope.time='';


        $scope.changeTime = function(val){
            $scope.time = val;
            console.log($scope);
        };
});

指令:

(function () {
    var mod = angular.module('matrixarCalendar', []);

    mod.directive('datepicker', function ($compile) {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elem, attrs, ngModelCtrl) {
                var updateModel = function (dateText) {
                    // call $apply to bring stuff to angular model
                    scope.$apply(function () {
                        ngModelCtrl.$setViewValue(dateText);
                    });

                };

                scope.updateDatePicker = function () {
                    $compile($.datepicker.dpDiv.append($('#custom-footer').contents().text()))(scope);
                 };
                var options = {
                    dateFormat: 'dd/mm/yy',
                    numberOfMonths: 2,
                    autoSize: true,
                    autoclose: true,
                    minDate: new Date(),
                    onSelect: function (dateText) {
                        updateModel(dateText);
                    }
                };
                elem.datepicker(options);
            }
        };
    });
}());

CSS:

.selected {
    background: red;
}

最佳答案

为每个 header 添加以下规则:

<th style="width:20px;" class="{{time == '0'? 'selected' : ''}}" ng-click="changeTime('0')">Journée</th>
<th style="width:20px;" class="{{time == '1'? 'selected' : ''}}" ng-click="changeTime('1')">Matin</th>
<th style="width:20px;" class="{{time == '2'? 'selected' : ''}}" ng-click="changeTime('2')">Midi</th>
<th style="width:20px;" class="{{time == '3'? 'selected' : ''}}" ng-click="changeTime('3')">Soir</th>

ng-class 期望来自 Controller 的 bool 表达式,因此如果你想使用 ng-class,你必须维护一个对象来记住哪个是事件的

关于javascript - 通过 ng-click 更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27423693/

相关文章:

javascript - glsify - 错误 'You may need an appropriate loader to handle this file type' ?

javascript - 将事件监听器添加到 Windows Phone 上的按钮 "Back, Start, Search"

javascript - AngularJs 将模型值+模型名称传递给 Controller ​​中的函数

javascript - 带有 key 对对象数组的 ng-options

javascript - 将动态创建的放置元素推送到 dom 树中的特定位置

javascript - 如何在 google map api 中更改缩放更改的标记

javascript - jQuery 轮播 slider

javascript - jQuery .prev() 的纯 javascript 替代品?

javascript - jQuery dataTable - 如何搜索与多个选项完全匹配的列

javascript - .checkboxradio ('refresh' )问题,无法与禁用的预加载字段一起使用