AngularJS - 过滤不列出空对象

时间:2015-08-26 14:11:28

标签: javascript angularjs

我有一个$scope.myData对象,在消息对象中有空值。

$scope.myData = [
{
   "ID" : "001",
   "deviceName":"sanjit",
   "Message" : "test1"
},{
   "ID" : "002",
   "deviceName":"gan",
   "Message" : "test2"
},{
   "ID" : "003",
   "deviceName":"dine",
   "Message" : ""
},{
   "ID" : "004",
   "deviceName":"sam",
   "Message" : "test4"
},{
   "ID" : "005",
   "deviceName":"dhar",
   "Message" : " "
},{
   "ID" : "006",
   "deviceName":"gau",
   "Message" : "test6"
},{
   "ID" : "007",
   "deviceName":"venk",
   "Message" : "test7"
}
]

我在dir-paginate标记中执行tr,如下所示

<br/><b> dir-paginate="dev in myData | filter:{deviceName:q,message:loc}></b>
<br/>

并使用两个输入过滤器

<br/>
Device Name: <b>ng-model="q"</b>
<br/>
Message: <b>ng-model="loc"</b>

当我使用ng-model="loc"在输入字段中搜索消息时,我将获得正确的结果列表(001,002,004,006,007)

当我清除字段&#34; loc&#34;我不会得到完整的结果,但只有上述5个结果(001,002,004,006,007),即清除之后 过滤字段&#34; loc&#34; ,我没有在消息中获得空值的对象。

请让我知道出了什么问题?

1 个答案:

答案 0 :(得分:1)

过滤器区分大小写,因此您需要将message更改为Message

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.myData = [
{
   "ID" : "001",
   "deviceName":"sanjit",
   "Message" : "test1"
},{
   "ID" : "002",
   "deviceName":"gan",
   "Message" : "test2"
},{
   "ID" : "003",
   "deviceName":"dine",
   "Message" : ""
},{
   "ID" : "004",
   "deviceName":"sam",
   "Message" : "test4"
},{
   "ID" : "005",
   "deviceName":"dhar",
   "Message" : " "
},{
   "ID" : "006",
   "deviceName":"gau",
   "Message" : "test6"
},{
   "ID" : "007",
   "deviceName":"venk",
   "Message" : "test7"
}
]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<html ng-app="myapp">
  
  <body ng-controller="MainCtrl">
    Device Name: <input ng-model="q">
    Message: <input ng-model="loc">

    <ul>
      <li ng-repeat="dev in myData | filter:{deviceName:q,Message:loc}">
        {{dev}}
      </li>
    </ul>
  </body>
</html>