使用函数AngularJs纳克核对 [英] AngularJs ng-checked using a function

查看:91
本文介绍了使用函数AngularJs纳克核对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,我试图在输入 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()"/>

DEMO

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆