简体   繁体   English

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

[英]Custom filter in Angularjs not working

I'm trying to implement custom filter in Angularjs. 我正在尝试在Angularjs中实现自定义过滤器。 But I'm not getting what is the problem in that. 但我没有得到那个问题。 Not getting the output as expected. 未按预期获得输出。 Here is my code: 这是我的代码:

script.js 的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 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>

Change the numeric cases to string values : 将数字大小写更改为字符串值:

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

Because 1 !== "1" . 因为1 !== "1"

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

the number you are passing is string not an integer. 传递的数字是字符串而不是整数。 change the switch case numbers to string 将开关案例编号更改为字符串

switch(gender){
      case "1" : return 'Male';
      case "2" : return 'Female';
    }

 var myApp = angular.module('myModule', []); myApp.filter("gender", function(){ return function(gender){ debugger 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; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div class="container" ng-app="myModule" 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> 

Change the numeric cases to string values : 将数字大小写更改为字符串值:

switch(gender){     
    case '1' : return 'Male';
    case '2' : return 'Female';
default: return 'Male';
}

You should convert that to string and also have the default result. 您应该将其转换为字符串,并且还具有默认结果。

AngularJS Filter AngularJS过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM