【问题标题】:ng-disabled not functioning when used with an expressionng-disabled 与表达式一起使用时不起作用
【发布时间】:2018-06-23 10:48:23
【问题描述】:
<h2>Your TO DOs</h2>
<div class="list-group" ng-repeat="i in tasks track by $index">
<div href="#" class="list-group-item" style="overflow:auto;">
<span id={{$index}} style="background:{{i.priority}};padding:1%; border:#bfb9b9; border-style:dotted;">{{i.note}} </span>
<h5 style="float:right;">
<span>
<button class="btn-xs" style="border:none;background: cornflowerblue;" ng-disabled = '{{i.priority==="red"}}' ng-click = 'impTask(i)' >Imp</button>
ng-disabled = '{{i.priority==="red"}}' 虽然通过 DOM 进行检查,但代码中的这一行不起作用,其值显示为“true”。
这是完整的代码
https://gist.github.com/7a3c7fd977c115638d386097f7c48b72.git
【问题讨论】:
标签:
angularjs
angularjs-ng-disabled
【解决方案1】:
你不应该使用带有 ng-disabled 的注解
改变
来自
ng-disabled = '{{i.priority==="red"}}'
到
ng-disabled = 'i.priority==="red"
演示
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.tasks = [{note:"Do the laundry", priority:'yellow'},{note:"Meeting at 10.00", priority:'yellow'}];
$scope.removeTask = function(task) {
var removedTask = $scope.tasks.indexOf(task);
$scope.tasks.splice(removedTask, 1);
};
$scope.addTask = function(){
if($scope.newTask!= null && $scope.newTask!= "" )
$scope.tasks.push({note:$scope.newTask,priority:'yellow'});
$scope.newTask= "";
};
$scope.impTask = function(task) {
var editedTask = $scope.tasks.indexOf(task);
$scope.tasks.splice(editedTask, 1);
task.priority= "red";
$scope.tasks.unshift(task);
};
});
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myApp">
<div class="container" ng-controller="personCtrl" style="margin:5%;">
<div>
<form ng-submit="addTask()">
<input type="text" onClick="this.select()" placeholder="enter the task" ng-model="newTask"/>
<input type="submit" value="Add a new task" />
</form>
</div>
<h2>Your TO DOs</h2>
<div class="list-group" ng-repeat="i in tasks track by $index">
<div href="#" class="list-group-item" style="overflow:auto;">
<span id={{$index}} style="background:{{i.priority}};padding:1%; border:#bfb9b9; border-style:dotted;">{{i.note}} </span>
<h5 style="float:right;" >
<span>
<button class="btn-xs" style="border:none;background: cornflowerblue;" ng-disabled = 'i.priority==="red"' ng-click = 'impTask(i)' >Imp</button>
</span>
<span>
<button class="btn-xs" style="border:none;background: cornflowerblue; " ng-click = 'strikeTask(i)'>Done</button>
</span>
<span>
<button class="btn-xs" style="border:none;background: cornflowerblue;" ng-click = 'editTask(i)' >Edit</button>
</span>
<a ng-click="removeTask(i)" style="padding-left:5px; padding-right:5px; color: red; font-weight: 800; background:#dad4d4";>X</a>
</div>
</div>
</body>
</html>