Javascript Angular.js:ng基于对象数组中的子字符串禁用
在我的项目中,我有一些待办事项。如果用户使用子字符串Javascript Angular.js:ng基于对象数组中的子字符串禁用,javascript,angularjs,input,Javascript,Angularjs,Input,在我的项目中,我有一些待办事项。如果用户使用子字符串clean输入todo项目,则应禁用输入,因此清洁、清洁、清洁等输入应禁用输入 在我的angular controller中,我编写了这个方法来禁用基于子字符串的输入 $scope.disableInput = function (todos) { todos.forEach(function (item) { if (item.toLowerCase().indexOf('clean') > -1) {
clean
输入todo项目,则应禁用输入,因此清洁、清洁、清洁等输入应禁用输入
在我的angular controller中,我编写了这个方法来禁用基于子字符串的输入
$scope.disableInput = function (todos) {
todos.forEach(function (item) {
if (item.toLowerCase().indexOf('clean') > -1) {
return true;
} else {
return false;
}
});
};
在我的html文件中,我将ng disabled
添加到输入中
<form ng-submit="addTodo();">
<input type="text" ng-model="todoText" ng-disabled="disableInput(todos)">
<input type="submit" value="add">
</form>
如果我手动将其设置为true,它会工作,但不会响应disableInput
方法的返回值。我想在每次提交新项目时检查todos
对象,并基于子字符串禁用它
如何从disableInput
方法将true
值返回到模板,以便禁用输入?
完整的示例是从中采用的,并添加了禁用功能。在您的函数(下面)中,您正在迭代一个集合,并在第一个对象上返回bool,这应该会引起一些注意
$scope.disableInput = function (todos) {
todos.forEach(function (item) {
if (item.toLowerCase().indexOf('clean') > -1) {
return true;
} else {
return false;
}
});
})
'我想在每次提交新项目时检查todos对象'
在添加todo时执行的函数中添加检查似乎是一个明智的选择
现在你不必重复你的待办事项
var disableInput = function (input) {
if(input.toLowerCase().indexOf('clean') > -1){
return true;
}
return false;
};
因此,您需要向作用域添加一个变量,以跟踪输入是否被禁用
$scope.disabled;
现在,在addTodo函数(从小提琴中抓取)中,您应该在刚刚输入的内容上添加“clean”字符串的检查
$scope.addTodo = function() {
$scope.todos.push({
text:$scope.todoText,
done:false
});
//Set the scope variable you just made to the result of the above function
$scope.disabled = disableInput($scope.todoText);
$scope.todoText = '';
};
所以最后在你的分数上;将禁用的变量分配给ng disabled
<input type="text" ng-model="todoText" size="30"
placeholder="add new todo here" ng-disabled="disabled">
这是我以前玩过的你的改良小提琴
希望这能有所帮助谢谢你的建议。有没有办法避免使用$scope变量
disable
?我这样问是因为在整个项目中,我在同一页面上显示多个用户,每个用户有一个输入,只有一个特定用户的字段应该被禁用。当$scope.disable
全部禁用时。对不起,为了避免复杂性,我没有提到这一点。