0

所以我想顯示,天氣用戶是活躍或不活躍。數據通過布爾形式的api傳遞。所以它會簡單地說用戶是真實的或者用戶是假的。如果數據返回true,我想顯示'Active',如果用戶返回false,我想顯示'Not Active'。解決方案我做了一個簡單的工作,但這是在應用程序中重複使用,我想做一個角度過濾器來完成這項任務。 我是一個角度和Java腳本的初學者,並沒有創建過濾器。用於從api返回true false的角度過濾器?

這是我的if else,並顯示在HTML 它這個我我要如何使用eventuatlly

<label>{{ $ctrl.myValu | truefalse}}</label> 

$onInit =() => { 
 
     let self = this; 
 

 
     this.siUser.current().then((userData) => { 
 
      let actingAsTenant = userData.user.apikey; 
 

 
      if (actingAsTenant) { 
 

 
       self.siTenant.current().then((tenant) => { 
 
        self.tenant = tenant; 
 
        
 
        ///starts 
 
        if (self.tenant.active == true) { 
 
         self.tenant.active = 'Active' 
 
        } 
 
        else if (self.tenant.active == false){ 
 
         self.tenant.active = 'Not Active' 
 
        } 
 
        //end 
 
        
 
       }, (err) => { 
 
        self.siAlertDialog.error(err.data.message) 
 
       }) 
 

 
      } 
 
     }, (err) => { 
 
      self.siAlertDialog.error('Error rendering tenant details'); 
 
     }); 
 
    }
<strong>{{$ctrl.tenant.active}}</strong>

+0

我想創建一個角度濾波器,所以它的可重複使用我的代碼上面工作。所以我想創建一個true或false的過濾器,最終看起來像這樣

回答

1
For this you need to create filter. Try to with the given code 

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 

     <title>Ristorante Con Fusion: Menu</title> 
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
    </head> 
    <body> 

     <div ng-app="myapp" ng-controller="userController" > 
     {{ myValu | truefalse }} 

     <script> 
    var app = angular.module('myapp', []); 

    app.filter('truefalse', function(){ 
     return function(text){ 
      return text ? "Active" : "Not Active"; 
     } 
     }) 
app.controller('userController', function($scope) { 
     $scope.myValu = true; 
}); 
     </script> 
    </body> 
    </html> 
+0

我想創建一個角度過濾器! ,我的代碼正在工作。 –

+0

有些事情是這樣的

+0

爲此,您需要創建過濾器。嘗試使用更新後的代碼 – Arun

0

您可以使用三元運算符:

JS

///starts 
     self.tenant.active == true ? 'Active' : 'Not Active'; 
//end 

OR

在角表達

<strong>{{ $ctrl.tenant.active ? 'Active' : 'Not Active' }}</strong> 
+0

我的代碼工作,我想創建一個angualr過濾器,所以我可以只是過濾器,而不是編碼。像這樣的東西

+0

去過濾器(作爲過濾器的功能):'$ scope.truefalse = function(item){ return item? '活躍':'不活躍'; };' ** **或' app.filter( 'truefalse',函數(){ 恢復功能(項目){ 退貨項目 '活動': '不活動'; }} ) ;' –