使用函数AngularJs纳克核对 [英] AngularJs ng-checked using a function
问题描述
我有一个表格,我试图在输入 NG-检查=someFunction()
来使用函数,我不知道这是可能的,或者我做错了什么。我有函数在我的控制器,我考虑调用它。至于功能,我认为它肯定工作和NG-检查被解雇,但返回true或false不会改变任何东西。所以,问题是有没有在'NG-检查使用功能的方法吗?
$ scope.multiAnswers =功能(答案,optionId){
angular.forEach(答案,功能(答案键){
如果(answer.option_choice_id == optionId){
返回true;
}
}); 返回false;
};
NG-检查
并与职能的工作。这里是一个演示:
$ scope.getCheckedFalse =功能(){
返回false;
};$ scope.getCheckedTrue =功能(){
返回true;
};
HTML:
<输入类型=复选框NG-检查=getCheckedFalse()/>
<输入类型=复选框NG-检查=getCheckedTrue()/>
您的问题是,您从不返回真正在函数的结尾。 返回true;
里面angular.forEach没有帮助
尝试:
$ scope.multiAnswers =功能(答案,optionId){
VAR的returnValue = FALSE;
angular.forEach(答案,功能(答案键){
如果(answer.option_choice_id == optionId){
的returnValue = TRUE;
返回;
}
}); 返回的returnValue;
};
看起来,我们不能从angular.forEach突破:角JS休息的ForEach
要提高性能,立即打破时, answer.option_choice_id == optionId
为真正。你可以尝试jQuery的 $。每个或使用JavaScript的vanilar(循环)。
I have a form and I am trying to use a function in input ng-checked="someFunction()"
and I don't know if this is possible or I am doing something wrong. I have function in my controller and I am calling it in view. As far as the function I think it's definitely working and the ng-checked is firing it but returning true or false doesn't change anything. So the question would be is there a way to use function in 'ng-checked' ?
$scope.multiAnswers = function (answers, optionId) {
angular.forEach(answers, function (answer, key) {
if (answer.option_choice_id == optionId) {
return true;
}
});
return false;
};
ng-checked
does work with functions. Here is a demo:
$scope.getCheckedFalse = function(){
return false;
};
$scope.getCheckedTrue = function(){
return true;
};
Html:
<input type="checkbox" ng-checked="getCheckedFalse()"/>
<input type="checkbox" ng-checked="getCheckedTrue()"/>
Your problem is that you never return true at the end of the function. return true;
inside angular.forEach does not help.
Try:
$scope.multiAnswers = function (answers, optionId) {
var returnValue = false;
angular.forEach(answers, function (answer, key) {
if (answer.option_choice_id == optionId) {
returnValue = true;
return;
}
});
return returnValue;
};
It looks like that we cannot break from angular.forEach: Angular JS break ForEach
To improve performance to break immediately when answer.option_choice_id == optionId
is true. You could try jQuery $.each or using vanilar javascript (for loop).
这篇关于使用函数AngularJs纳克核对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!