2017-03-16 4 views
0

私のデータテンプレートは、次のようになり、NG-変更内側jsの角度フィルタは

scope.items=[{"name:"John","score":1},{"name":"Rick","score":5},{"name":"Peter","score":2}] 

私は値0,1,2,3,4,5と選択ボックスがあり、上のベースitems objectをフィルタリングしたいと思いますscore value

scope.scores= [ 
       { 
        "name":"condition 0", 
        "value":"0" 
       }, 
       { 
        "name":"condition 1", 
        "value":"1" 
       }, 
       { 
        "name":"condition 2", 
        "value":"condition 2" 
       }, 
       { 
        "name":"condition 3", 
        "value":"3" 
       }, 
       { 
        "name":"condition 4", 
        "value":"4" 
       }, 
       { 
        "name":"condition 5", 
        "value":"5" 
       } 
      ] 

HTML

<select id="scoreS" ng-model="score" ng-change="updateScore()"> 
        <option 
          ng-repeat="score in scores" 
          value="{{score.value}}">{{score.name}}</option> 
       </select> 

は、だから私はを選択すると、ドロップダウンリストで選択します私のドロップダウンボックスからitemsをフィルタリングして、スコア(キー)0に一致するオブジェクトのみを表示し、ドロップダウンから1を選択し、スコア(キー)1のアイテムのみと一致するようにする必要があります。 updateScore()関数の中でこれをどうやってやっていますか?

答えて

0

使用ダウン​​

<div ng-repeat="item in items | filter :score">

デモ

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 
$scope.scores= [ 
 
       { 
 
        "name":"condition 0", 
 
        "value":"0" 
 
       }, 
 
       { 
 
        "name":"condition 1", 
 
        "value":"1" 
 
       }, 
 
       { 
 
        "name":"condition 2", 
 
        "value":"2" 
 
       }, 
 
       { 
 
        "name":"condition 3", 
 
        "value":"3" 
 
       }, 
 
       { 
 
        "name":"condition 4", 
 
        "value":"4" 
 
       }, 
 
       { 
 
        "name":"condition 5", 
 
        "value":"5" 
 
       } 
 
      ] 
 
$scope.items=[{"name":"John","score":1},{"name":"Rick","score":5},{"name":"Peter","score":2}] 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<select id="scoreS" ng-model="score" ng-change="updateScore()"> 
 
    <option ng-repeat="score in scores" value="{{score.value}}">{{score.name}}</option> 
 
    </select> 
 
    <div ng-repeat="item in items | filter :score"> 
 
    {{item.name}}= {{item.score}} 
 
    </div> 
 
</div>

1

をドロップするように応じてデータをフィルタするfilterオプションは次のように試してみてください。それはdrop-down

var app = angular.module('anApp', []); 
 
app.controller('aCtrl', function($scope) { 
 
$scope.items=[ 
 
    {"name":"John","score":1} 
 
,{"name":"Rick","score":5} 
 
,{"name":"Peter","score":2} 
 
] 
 

 
$scope.scores= [ 
 
       { 
 
        "name":"condition 0", 
 
        "value":"0" 
 
       }, 
 
       { 
 
        "name":"condition 1", 
 
        "value":"1" 
 
       }, 
 
       { 
 
        "name":"condition 2", 
 
        "value":"condition 2" 
 
       }, 
 
       { 
 
        "name":"condition 3", 
 
        "value":"3" 
 
       }, 
 
       { 
 
        "name":"condition 4", 
 
        "value":"4" 
 
       }, 
 
       { 
 
        "name":"condition 5", 
 
        "value":"5" 
 
       } 
 
      ] 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> 
 

 
<div ng-app="anApp" ng-controller="aCtrl as vm"> 
 
<select id="scoreS" ng-model="score" ng-options="score.value as score.name for score in scores "> 
 
       </select> 
 
    <div ng-repeat="item in items| filter:{score:score}"> 
 
    <span>{{item.name}}</span> 
 
    </div> 
 
</div>

で活用 ng-optionsの代わり ng-repeatです