Angular Search for value in JSON and display corresponding data

后端 未结 1 712
挽巷
挽巷 2021-01-31 06:39

What i am trying to do is simple. A user enters a value, on button click, my JS calls a service to retreive my JSON data and perform a search on the value entered against the JS

1条回答
  •  佛祖请我去吃肉
    2021-01-31 07:18

    The key is just to iterate over the data object while observing the correct structure for accessing the values.

    Your search function could look like this:

    $scope.results = [];
    $scope.findValue = function(enteredValue) {     
        angular.forEach($scope.myData.SerialNumbers, function(value, key) {
            if (key === enteredValue) {
                $scope.results.push({serial: key, owner: value[0].Owner});
            }
        });
    };
    

    Notice that I'm pushing the results into an array. You can setup an ng-repeat in the view which will use this to present a live view of the results:

    
    

    Results

    • Serial number: {{result.serial}} | Owner: {{result.owner}}

    Demo

    0 讨论(0)
提交回复
热议问题