dongpu7881 2017-03-06 05:33
浏览 95
已采纳

Angularjs中的自定义过滤器无法正常工作

I'm trying to implement custom filter in Angularjs. But I'm not getting what is the problem in that. Not getting the output as expected. Here is my code:

script.js

var myApp = angular.module('myModule', []);
myApp.filter("gender", function(){
  return function(gender){
    switch(gender){
      case 1 : return 'Male';
      case 2 : return 'Female';
    }
  }
});

myApp.controller('myController', function($scope){
var employees = [
    { name : 'Raghu', gender : '1', salary : 84000.779 },
    { name : 'Anil', gender : '1', salary : 78000 },
    { name : 'Ramya', gender : '2', salary : 118000 },
    { name : 'Shwetha', gender : '2', salary : 68000 },
    { name : 'Chethan', gender : '1', salary : 168000 }
];
$scope.employees = employees;

});

page.html

<div class="container" ng-controller="myController">
        <h1>Angular Example Ten</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Gender</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="employee in employees">
                    <td>{{ employee.name }}</td>
                    <td>{{ employee.gender | gender }}</td>
                    <td>{{ employee.salary }}</td>
                </tr>
            </tbody>
        </table>
    </div>
  • 写回答

3条回答 默认 最新

  • dst3605528 2017-03-06 05:38
    关注

    Change the numeric cases to string values :

    case '1' : return 'Male';
    case '2' : return 'Female';
    

    Because 1 !== "1".

    console.log('1 !== "1" ::::', 1 !== "1"); // true

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度