AngularJS - 无法检索json元素

时间:2016-01-10 09:27:28

标签: javascript angularjs json

在我的角度网络应用程序中,我正在尝试检索json元素(consolidationScopeId),但它打印为未定义。

$scope.consolidationScopeChanged = function(consolidationScope) {
console.log("consolidationScope : " +consolidationScope);
 console.log("consolidationScope : " +$scope.reportType.consolidationScope);
console.log("consolidationScope.consolidationScopeId : " +$scope.reportType.consolidationScope.consolidationScopeId);

}

我使用console.log记录了它

Plunker:

https://plnkr.co/edit/CRypbp6AIXEAYbeJupoh?p=preview

2 个答案:

答案 0 :(得分:2)

您的数据在模型中设置为字符串。

试试这个

 console.log(JSON.parse(consolidationScope).frequencies);

DEMO

而是使用 ngOptions ,他会为您绘制地图

喜欢这个

<select id="Select2" class="form-control" ng-model="reportType.consolidationScopeCode" data-ng-options="conScopeFreqStartDates as conScopeFreqStartDates.consolidationScopeLabel  for conScopeFreqStartDates in conScopeFreqStartDates" ng-change="consolidationScopeChanged(reportType.consolidationScopeCode)">
      <option value=""></option>
</select>

JS

  $scope.consolidationScopeChanged = function(consolidationScope) {
    console.log(consolidationScope.frequencies);
  }

DEMO

答案 1 :(得分:1)

请解析你的json对象。使用JSON.parse

&#13;
&#13;
-2147483651
&#13;
&#13;
&#13;