javascript - 如何将css放在angular js中的选定行中?

标签 javascript html css angularjs

我正在用 angularjs 创建一个待办事项列表..它几乎完成了..我的问题是当我点击编辑按钮时,我想通过添加一个 css 类“黄色”来突出显示处于编辑模式的整行... 但我不知道该怎么做...

谁能告诉我我的编码方式是正确的还是错误的..拜托

jsfiddle 链接

http://jsfiddle.net/mcVfK/1338/

这是我的代码

html

<div ng-app="myapp">
<div class="container" ng-controller="mainCtrl">
        <h3>Todo List</h3>
        <input type="text" class="form-control" placeholder="create your todos" ng-model="newItem">
        <p class="help-block text-center red" ng-show="!newItem && empty">*Fill the field.</p>
        <br>

        <table class="table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Todo</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="todoList in todoLists">
                    <td>{{$index+1}}</td>
                    <td>{{todoList.name}}</td>
                    <td>{{todoList.edit}}</td>
                    <td><a class="btn {{disabled}} pull-right" href="" ng-click="remove(todoList)">delete</a>
                        <a class="btn {{disabled}} pull-right" href="" ng-click="edit($index)">edit</a> </td>
                    </tr>
                </tbody>
            </table>


            <button type="button" class="btn btn-primary btn-lg btn-block" ng-click="add()" ng-hide="editMode">ADD</button>
            <button type="button" class="btn btn-default btn-lg btn-block" ng-click="update(newItem)" ng-show="editMode">UPDATE</button>
        </div>
</div>

我的js文件

var app = angular.module("myapp", []);
app.controller("mainCtrl", ["$scope", "$rootScope", function($scope, $rootScope){
    $scope.empty = false;
    $scope.editMode = false;

    $scope.todoLists = [{name : "one", edit : "false"},{name : "two", edit : "false"}];

    $scope.add = function(){
        if(!$scope.newItem == ""){
            $scope.todoLists.push({name:$scope.newItem, edit:"false"});
            $scope.newItem = "";
            $scope.empty = false;
        }else{
            $scope.empty = true;
        };
    };

    $scope.remove = function(item){
        var index = $scope.todoLists.indexOf(item);
        $scope.todoLists.splice(index, 1);
    };

    $scope.edit = function(index){
        $rootScope.ind = index;
        $scope.newItem = $scope.todoLists[$rootScope.ind].name;
        $scope.editMode = true;
        $scope.disabled = "disabled";
        $scope.todoLists[index].edit = "true";
    };

    $scope.update = function(item){
        if(!$scope.newItem == ""){  
            $scope.todoLists[$rootScope.ind].name = item;
            $scope.todoLists[$rootScope.ind].edit = "false";
            $scope.editMode = false;
            $scope.newItem = "";
            $scope.disabled = "";
        }else{
            $scope.empty = true;
        };
    };


}]);

CSS文件

.yellow{
        background:yellow;
    }
    .red{
        color:red;
    }

最佳答案

您可以使用 AngularJS 提供的 ng-class 指令。 https://docs.angularjs.org/api/ng/directive/ngClass

改变

<tr ng-repeat="todoList in todoLists">

<tr ng-repeat="todoList in todoLists" ng-class="{yellow: editMode && $index == ind}">

关于javascript - 如何将css放在angular js中的选定行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35498034/

相关文章:

javascript - 超出页面高度的响应式菜单

javascript - 动态图像显示 - javascript 错误的 url 读取

html - 当我最小化页面时,主体颜色没有填满整个窗口

javascript - 如何为第二个列表项应用默认事件类

jquery - 我用来打开和关闭 div 的 JQuery 代码不起作用

php - echo Javascript 出现在警报中而不是处理中

javascript - 使用 jQuery 居中一个灵活的 div

javascript - 如何解决实际需要 componentWillMount 的用例?

c# - 如何在数据 ListView 中给出href值?

javascript - Base64 编码的图像在 IE 中不显示