【问题标题】:ng-if on ng-repeat that calls a functionng-if on ng-repeat 调用一个函数
【发布时间】:2016-03-08 07:08:21
【问题描述】:

我有一个笨蛋:https://plnkr.co/edit/OlLt9XC6cWYnus20ZEaz?p=preview

我有一个 ng-repeat,它调用一个函数并获得 .outcome 的结果,结果是真或假。但是,如果不返回这些值,则默认为 x。

我想做的是对 ng-repeat 值执行 ng-if 以便:

if outcome is true, show icon-true.
if outcome is false, show icon-cross.
if outcome is x, show icon-blank.

HTML:

<div ng-repeat="month in months">
    {[{getData(contents[item._id].contentHistory,year,month.n)}]}
    <!-- <i class="icon-cross" ng-if="getData(contents[item._id].contentHistory,year,month.n == false"></i>
    <i class="icon-true" ng-if="getData(contents[item._id].contentHistory,year,month.n == true"></i>
    <i class="icon-blank" ng-if="getData(contents[item._id].contentHistory,year,month.n == x"></i> -->
</div>

【问题讨论】:

  • 什么是x?是字符串吗?
  • @mgilson 是的。看看 plunker 中的控制器
  • 您需要与'x' 而不是x 进行比较,因为它不是变量而是值。并关闭getDatacall 的圆括号。如果那是纠正,代码工作正常。你的样式/图标有问题,我没有深入,尝试使用标签而不是图标。工作。

标签: javascript angularjs angularjs-ng-repeat ng-class angular-ng-if


【解决方案1】:

如果您只需要 2 个结果 - true、false 那么你应该使用ng-hideng-show

其他使用ng-switch

<ANY ng-switch="CALL YOUR EXPRESSION">
  <ANY ng-switch-when=true> INSERT TRUE-ICON CODE HERE</ANY>
  <ANY ng-switch-when=false> INSERT FALSE-ICON CODE HERE</ANY>
  <ANY ng-switch-default>INSERT DEFAULT-ICON CODE HERE</ANY>
</ANY

这样,函数只被调用一次。

PS:你的逻辑工作正常,(即使你不应该调用 getData 3 次)。摆脱我在评论中提到的错误。我尝试使用标签代替图标,它有效。

【讨论】:

  • 你有一个有效的例子吗?我似乎无法让开关正常工作。
  • 不要从 angular 站点复制和粘贴代码,因为我提供了一个 plunker,这高度表明我已经咨询了 angular 文档。
  • 抱歉,您的 plunker 有很多错误,这表明您没有查阅 angular 文档。您需要与 'x' 和 NOT x 进行比较,因为它不是变量而是值。并关闭 getData 调用的圆括号。如果那是纠正,代码工作正常。你的图标有问题,我没有深入,尝试使用标签而不是图标。工作。
【解决方案2】:

试试这个。定义一个结果变量并在函数中初始化它。

  $scope.outcome = "";
  $scope.getData = function(parameters){
    if(true)
       $scope.outcome = 'show icon-true';
    if(false)
        $scope.outcome = 'show icon-cross';

    }



<div ng-repeat="month in months">
    <i ng-class="{'show icon-true': outcome == true, 'show icon-cross':outcome == false,}"></i>
</div>

我为您的问题提出了另一种解决方案。

var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
    $scope.items = [
       {"name":"Ali","class":"a"},
       {"name":"Reza","class":"b"},
       {"name":"Majid","class":"c"}
     ];
  
  $scope.getData = function(param){
     if(param == 'a')
       return 'a';
     if(param == 'b')
       return 'b';
    
    }
    
    
  });
.a{
  color:red;
  }
.b{
  color:blue;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   
      <div ng-repeat="item in items">
        
        <p ng-class = "getData(item.class)">{{item.name}}</p>
       </div> 
 
</div>

【讨论】:

  • 谢谢,:outcome 是从哪里来的?我正在使用 {[{getData(contents[item._id].contentHistory,year,month.n)}]} 来获得我的结果。
  • @shershen 你是对的,这不是 ng-class 语法。
  • @hadiJZ - 否决票是在你更新之前,而且你还没有说结果来自哪里
【解决方案3】:

您可以使用三元运算符通过嵌套来检查所有三个条件。

<div ng-repeat="month in months">
<i ng-class="{getData(contents[item._id].contentHistory,year,month.n)==true ?'show icon-true': (getData(contents[item._id].contentHistory,year,month.n)==false ? 'show icon-cross' : 'show icon-blank')}"></i>

【讨论】:

    【解决方案4】:

    试试这个

    <div ng-repeat="month in months">
        <i ng-class="{getData(contents[item._id].contentHistory,year,month.n)==true ?'show icon-true': getData(contents[item._id].contentHistory,year,month.n)==false?'show icon-cross':'show icon-blank'}"></i>
    </div>
    

    希望对你有帮助。

    【讨论】:

    • 这完全忽略了 op 提到的第三个选项,因此这个解决方案首先错过了主题
    • @PatrickKelleter 我已经相应地更新了我的答案。请立即检查。
    • 这更好,因为它适合现在的问题,但它的性能较差,因为它必须调用该函数两次。但至少它现在是一个正确的答案,即使它不是最好的:)
    • 感谢回答,但调用函数3次效率非常低
    • 这总是默认为 show-blank - 你测试过吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    相关资源
    最近更新 更多