2016-06-22 5 views
0

こんにちは私は、角度jで新しいです。私は検索ボックスを作成したいと思います。ユーザーがすべてのキー入力を入力すると、私のリクエストはjsonに行き、検索ボックスの値と一致する結果を取得します。私はいくつかのことを試みましたが、この仕事をどうやってやるのか分かりません。以下のコードを確認してくださいjsonファイルからキーアップでデータを取得し、角度を使用してdivに追加します。

次のコードでは、一致する配列を$scope.keywordにプッシュしようとしました。私はこれを行う場合$scope.keywordng-repeatを使用して私のdivに一致する結果を表示します。

私に助けてください友人

コントローラJS

var appProduct = angular.module('assignment', []); 

appProduct.service('dataCollection', function($http) { 
    return { getData : function() { 
     return $http.get('js/data.json').then(function(res){ 
      return res.data; 
     }) 
    }} 
}) 

appProduct.controller('searchBar', ['$scope', '$http', 'dataCollection', function($scope, $http, dataCollection){ 

    $scope.items = []; 
    $scope.keyword = []; 

    dataCollection.getData().then(function(data){ 
    $scope.items = data; 
    }, function(data){ 
    console.log(data); 
    }) 

    $scope.keyword = function(key){ 
    console.log(key); 
    console.log($scope.items) 

    dataCollection.getData().then(function(data){ 
    //$scope.items = data; 

     angular.forEach(data, function(value, key){ 

     if (value.brandname == key) { 
     $scope.keyword.push(value) 
     } 

    }); 


    }, function(data){ 
    console.log(data); 
    }) 

    } 

}]) 

答えて

0

index.htmlを

<body ng-app> 
    <div ng-controller="Controller"> 
     City: <input type="text" ng-model="name"><br> 
     Order by: 
     <a href="" ng-click="predicate = 'name'">name</a> 
     <a href="" ng-click="predicate = 'polulation'">population</a> 
     <ul> 
      <li ng-repeat="city in cities | filter: name | orderBy: predicate"> 
      {{ city.name }} | {{ city.population }} 
      </li> 
     </ul> 
    </div> 
</body> 

external.js

function Controller($scope){ 
    $scope.cities = [ 
     {name: "Shanghai", population: "16 060 307"}, 
     {name: "Lagos", population: "13 969 284"}, 
     {name: "Karachi", population: "13 907 015"}, 
     {name: "Istanbul", population: "12 478 447"}, 
     {name: "Mumbai", population: "12 111 194"}, 
     {name: "Moscow", population: "11 821 873"}, 
     {name: "São Paulo", population: "11 716 620"}, 
     {name: "Beijing", population: "11 070 654"}, 
     {name: "Guangzhou", population: "11 007 835"}, 
     {name: "Delhi", population: "10 520 000"}, 
     {name: "Lahore", population: "10 467 400"}, 
     {name: "Shenzhen", population: "10 442 426"}, 
     {name: "Seoul", population: "9 761 407"}, 
     {name: "Jakarta", population: "9 341 844"}, 
     {name: "Tianjin", population: "8 981 087"}, 
     {name: "Chennai", population: "8 967 665"}, 
     {name: "Tokyo", population: "8 922 949"}, 
     {name: "Cairo", population: "8 906 039"}, 
     {name: "Dhaka", population: "8 873 017"}, 
     {name: "Mexico City", population: "8 859 036"}, 
     {name: "Lima", population: "8 754 000"}, 
     {name: "Kinshasa", population: "8 425 970"}, 
     {name: "Bangalore", population: "8 336 697"}, 
     {name: "New York", population: "8 308 369"}, 
     {name: "London", population: "8 280 925"}, 
     {name: "Bangkok", population: "8 244 535"}, 
     {name: "Tehran", population: "8 220 237"}, 
     {name: "Dongguan", population: "7 776 845"}, 
     {name: "Bogotá", population: "7 681 700"}, 
     {name: "Ho Chi Minh City", population: "7 310 691"}, 
     {name: "Faisalabad", population: "7 108 100"}, 
     {name: "Hong Kong", population: "6 844 100"}, 
     {name: "Hanoi", population: "6 809 970"}, 
     {name: "Hyderabad", population: "6 434 373"}, 
     {name: "Wuhan", population: "6 429 923"}, 
     {name: "Rio de Janeiro", population: "6 151 622"}, 
     {name: "Foshan", population: "6 150 000"}, 
     {name: "Baghdad", population: "5 570 585"}, 
     {name: "Ahmedabad", population: "5 399 200"}, 
     {name: "Singapore", population: "5 391 028"}, 
     {name: "Shantou", population: "5 188 286"}, 
     {name: "Riyadh", population: "5 131 967"}, 
     {name: "Saint Petersburg", population: "5 112 018"} 
    ]; 
    $scope.predicate = 'population'; 
} 

demo click here

関連する問題