2015-12-07 16 views
2

我查看了一些響應,無法爲我的生活弄清楚爲什麼我的代碼不工作!使用自定義過濾器時ng-bind失敗

我想將一個簡單的自定義過濾器應用於從控制器傳遞的變量。 room-price值顯示正常。但是,當我添加price-per-night過濾器時,該值將消失。

任何人都可以提出解決方案嗎? 由於

模板

<div class="h3 left-align p1" ng-bind="room.price | price-per-night"></div> 

控制器

routerApp.controller('MyController', function($scope) { 
    $scope.rooms = [ 
    { 
     name: 'Basic', 
     price: 50 
    }] 
}); 

過濾

routerApp.filter("price-per-night", 
    function() { 
     return function(price) { 
      var output = '£' + price + ' ' + 'per night'; 
      return output; 
     }; 
    } 
); 

回答

2

爲您的過濾器命名pricePerNight-不得在名稱:

routerApp.filter("pricePerNight", 
    function() { 
     return function(price) { 
      var output = '£' + price + ' ' + 'per night'; 
      return output; 
     }; 
    } 
); 

(您仍然可以使用price-per-night在你的HTML)