我正在用 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/