如何以角度注入大写过滤器?

时间:2015-03-13 06:47:38

标签: javascript angularjs

我正在尝试将大写过滤器作为依赖注入注入控制器,我尝试注入orderByFilter并且它完美地工作。 你可以参考我的样本Plunker项目

PlunkerLink

 var app = angular.module("myApp", [])
  .controller('HomeController',function($scope,orderByFilter,uppercaseFilter)
{
  $scope.sortOrder = '-id';
  $scope.upperCase = 'fName';
   $scope.users = [
    { id: 1, fName: 'Hege', lName: "Pege" },
    { id: 2, fName: 'Kim', lName: "Pim" },
    { id: 3, fName: 'Jack', lName: "Jones" },
    { id: 4, fName: 'John', lName: "Doe" },
    { id: 5, fName: 'Peter', lName: "Pan" }
];
 $scope.users = orderByFilter($scope.users, $scope.sortOrder);
 $scope.users = uppercaseFilter($scope.users, $scope.upperCase);

});

2 个答案:

答案 0 :(得分:1)

您必须手动处理对象数组,因为upperCase过滤器仅适用于字符串。在你的情况下,它可以是这样的例如:

$scope.users = $scope.users.map(function(obj) {
    obj[$scope.upperCase] = obj[$scope.upperCase].toUpperCase();
    return obj;
});

演示: http://plnkr.co/edit/QaCisLJAUI1eWbZNTtdz?p=preview

答案 1 :(得分:0)

它可能对你有帮助。

HTML code-sample

<span>{{ users.fName | limitTo : 3 | uppercase }}</span>
<span ng-bind="users.fName | limitTo : 3 | uppercase "></span>

AngularJs代码示例

<script>
  angular.module("myApp").controller("myController"), function($filter){

    var result = $filter(‘limitTo’)(users.fName, 3);
    $filter(‘uppercase’)(result);
    ...
  }
</script>