对数组内的对象属性进行角度过滤

时间:2016-11-15 16:46:51

标签: javascript angularjs angularjs-ng-repeat angularjs-filter

我在#include <stdio.h> #include <sys/types.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdlib.h> #include <strings.h> #include <string.h> #include <unistd.h> #define BUFFER_SIZE 80 static void error(char *msg){ perror(msg); exit(1); } typedef union UNIT{ short s; char c[sizeof(short)]; }unit; int main(int argc, char ** argv){ int socketfd, portno, n, length; struct sockaddr_in server, from; struct hostent *host; char buffer[BUFFER_SIZE]; unit un; un.s=0x0102; if(argc!=3){ fprintf(stderr,"Error starting the client, please start client as: %s hostname port\n", argv[0]); exit(0); } if((host=gethostbyname(argv[1]))==NULL) error("ERROR UDP_CLI_001 - no such host defined, please check your /etc/hosts file"); portno=atoi(argv[2]); if((socketfd=socket(AF_INET,SOCK_DGRAM,0))<0) error("ERROR UDP_CLI_002 - can not create socket"); bzero((char *)&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(portno); bcopy((char *)host->h_addr,(char *)&server.sin_addr,host->h_length); length=sizeof(server); if(sizeof(short)!=2){ exit(0); } n=sendto(socketfd,un.c,strlen(un.c),0,(struct sockaddr *)&server,length); if(n<0) error("ERROR UDP_CLI_003 - can not send to server"); bzero(buffer,BUFFER_SIZE); n=recvfrom(socketfd,buffer,BUFFER_SIZE,0,(struct sockaddr *)&from, &length); if(n<0) error("ERROR UDP_CLI_004 - can receive from server"); printf("%s",buffer); return 0; } 内有一组对象。该对象具有$scope和'attributes'属性,name是一个对象。我有一个文本字段,我需要绑定到一个模型,该模型应该用于根据attributesname过滤状态。但是,以下代码不会过滤项目。哪里出错了。

我正在使用Angularjs 1.5.8版本

noOfCitizens

1 个答案:

答案 0 :(得分:2)

更改您的ng-model指令以及传递给filter管道的选项,如下所示,

ng-model="ctrl.stateFilter"

ng-repeat="state in ctrl.states | filter : ctrl.stateFilter"

检查以下代码段。

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController);

function DefaultController() {
  var vm = this;
  vm.states = [];
  var mp = {
    name: "MP",
    attributes: {
      "name": "MP",
      "noOfCitizens": "~ 900000"
    }
  };

  var ts = {
    name: "TS",
    attributes: {
      "name": "TS",
      "noOfCitizens": "~ 8000"
    }
  };

  var vs = {
    name: "VS",
    attributes: {
      "name": "VS",
      "noOfCitizens": "~ 8000"
    }
  };

  vm.states.push(mp);
  vm.states.push(ts);
  vm.states.push(vs);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController as ctrl">
    <div style="margin-left: 10px">
      <input type="text" ng-model="ctrl.stateFilter" placeholder="filter">
    </div>
    <div class="col-lg-3" ng-repeat="state in ctrl.states | filter : ctrl.stateFilter">
      <h2>{{state.name}}</h2>
      <ul>
        <li>Name: {{state.attributes.name}}</li>
        <li>No Of Citizens: {{state.attributes.noOfCitizens}}</li>
      </ul>
    </div>
  </div>
</div>