我无法根据angularjs中的键值对从json对象中获取特定数据

时间:2017-09-20 11:38:27

标签: angularjs

我无法根据angularjs中的键值对从json对象中获取特定数据。

    var app = angular.module("myApp", []);
    app.controller("myDialogController", function($http, $scope) {
        alert("nishant is here");

        $scope.onSubmit = function() {
            alert("inside submit function");
        }

        $scope.displayData = function() {
            alert("nishant is here on load");
        }
    });

    app.controller("myCntrl", function($scope, $http) {
        alert("velson is here");
        $scope.displayData = function() {
            alert("inside the velson");
            $http.get("retrieve_1.jsp")
                .then(function(response) {
                    $scope.name = response.data;
                    //$scope.value = $scope.name.id;
                    //alert($scope.value);


                });
        }
    })

这里$ scope.name包含:

[{"id":"1","emp_id":"2010","sales_force_id":"sales_force_id_test_url","type_of_request":"New","rfp_rfi":"1","closure_deadline":"2017-09-18 15:23:37.0","mode_of_submission":"S","submission_date":"2017-09-18 15:23:37.0","clarification_date":"2017-09-18 15:23:37.0","extention_date":"2017-09-18 15:23:37.0","region":"Region 1","item_status":"Pending","participants_status":"Pending","reviewer_status":"Pending","description":"null","bid_owner":"null"}]

但是我无法从中获取id。帮助我做到这一点。

3 个答案:

答案 0 :(得分:0)

$scope.name是一个对象列表

获取如下:

$scope.value = $scope.name[0].id;

同样值得将$scope.name重构为$scope.names,因为您获得了列表并且对您来说很清楚。

$scope.names = response.data;
$scope.value = $scope.names[0].id;

答案 1 :(得分:0)

您可以像这样获取id值 var id = $ scope.name [0] .id

答案 2 :(得分:0)

试试这个

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

function MyCtrl($scope) {
    $scope.data =  [{"id":"1","emp_id":"2010","sales_force_id":"sales_force_id_test_url","type_of_request":"New","rfp_rfi":"1","closure_deadline":"2017-09-18 15:23:37.0","mode_of_submission":"S","submission_date":"2017-09-18 15:23:37.0","clarification_date":"2017-09-18 15:23:37.0","extention_date":"2017-09-18 15:23:37.0","region":"Region 1","item_status":"Pending","participants_status":"Pending","reviewer_status":"Pending","description":"null","bid_owner":"null"}];
}
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <table borderd>
  <tr>
    <th>Id</th>
    <th>Emp Id</th>
    <th>Sales Force Id</th>
    <th>Item Status</th>
  </tr>
  <tr ng-repeat="i in data">
    <td>{{i.id}}</td>
    <td>{{i.emp_id}}</td>
    <td>{{i.sales_force_id}}</td>
    <td>{{i.item_status}}</td>
  </tr>
</table>
</div>

这可以帮到你