如何在控制器中为特定值编写过滤器

时间:2015-08-24 19:07:26

标签: angularjs

如何在app.js的控制器中重写下面的代码行。我怎么能写一个函数,我在其中过滤mustShow var的长度大于零

    **choice | filter: {mustShow: 'yes'}).length > 0**

  <body ng-app="Compensation">
 <script src="https://code.angularjs.org/1.4.3/angular.js"></script>
  <script src="https://code.angularjs.org/1.4.3/angular-route.js"></script>
  <script src="app.js"></script>
  <div ng-controller="MainCtrl">
    <div data-ng-repeat="choic in choice">
      <form>
        <input ng-model="choic.mustShow" class="label_align" type="radio"   name="customer" value="no" ng-change="change()" >
        <div class="mediumSubhead"> Information housed </div>
        <br/>
        <input ng-model="choic.mustShow" class="label_align" type="radio" name="customer" value="yes">
        <div class="mediumSubhead"> Information housed outside </div>
      </form>
    </div>
    **<div ng-show="(choice | filter: {mustShow: 'yes'}).length > 0">**
      <input name="first_name" class="text_box_space" type="text" value="" style="color: black;" size="25" width="200px">
    </div>
  </div>
</body>
  var app = angular.module('EquityCompensation', []);
  app.controller('MainCtrl', function($scope) {
  $scope.choice = [{id: 'choic1'},{id: 'choic2'}];

 });

1 个答案:

答案 0 :(得分:0)

您应该在控制器中注入$filter,然后将其用作:

$filter('choice')({mustShow: 'yes'}).length > 0

这样的事情:

var app = angular.module('EquityCompensation', []);
app.controller('MainCtrl', function ($scope, $filter) {
    var choice = $filter('choice')({mustShow: 'yes'});
    // Do something with choice
});