2017-03-06 79 views
3

我想在Angularjs中實現自定義過濾器。但我沒有得到那個問題。沒有按預期得到輸出。這裏是我的代碼:Angularjs中的自定義過濾器不工作

的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> 
+0

是否有在控制檯的任何錯誤? – Smit

+0

如果你提到了你所得到的輸出,這將會很有幫助。 –

+0

沒有錯誤,性別列是空的.. –

回答

6

更改數字情況下字符串值:

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

因爲1 !== "1"

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

2

更改數字情況下字符串值:

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

你應該將其轉換成字符串,也有默認的結果。

AngularJS Filter

3

你逝去的數量是字符串不是整數。改變開關的情況下,數字串

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>

相關問題