【问题标题】:How to show json response in html drop down list using angular js如何使用angular js在html下拉列表中显示json响应
【发布时间】:2016-10-15 20:26:52
【问题描述】:

我正在尝试使用 Angular JS 在 HTMl 的下拉列表中显示 JSON 响应。

我正在尝试在 html 的代理下拉列表中调用“短名称”。 有任何想法吗。提前致谢。

我的 JSON 代码

                       [{"id":1,"agencyCode":"006","agencyDescription":"null","shortName":"NJTA","cscId":1,"iagAgencyTypeId":4,"comments":"null","whoCreated":"admin","createdDate":"2016-06-13","whoUpdated":"admin","updatedDate":"2016-06-13"}]

HTML 代码

            <div class="dashboard_searcharea">
              <div class="col-md-3" ng-repeat="shortname in agencies"> Agency :
                <select class="text_field_style smallfield">
                <option value="shortname">  agencies: {{agencies.shortname}} </option>
                </select> 
              </div>
              <div class="col-md-3"> Events :
                <select class="text_field_style smallfield">
                </select>
              </div>

角度 JS

   Dashboard.controller('homeController',homeController); 
   function homeController($scope,$location,$http) {
    $scope.dt = new Date(); 
      $scope.popup1 = {
        opened: false
      };
      $scope.open1 = function() {
        $scope.popup1.opened = true;
      };

    $http.get("http://localhost:8080/EtbosAdmin/agencies")
      .success(function(response) {                    
         $scope.metrics=response.series[0].shortname; 

【问题讨论】:

    标签: javascript html angularjs json


    【解决方案1】:

    要达到您的预期效果,请按照以下步骤操作

    1.在迭代中使用不同的名称和键值以避免混淆..为了测试我在机构中使用了 JSON 和变量 x。

    2.Key value 是 shortName ,所以我们需要通过 x.shortName 来获取 value。

    我已使用 $scope.agencies 中的 JSON 进行测试

    <div class="dashboard_searcharea">
              <div class="col-md-3" ng-repeat="x in agencies"> Agency :
                <select class="text_field_style smallfield">
                          <option value="shortname">  agencies: {{x.shortName}} </option>
                          </select>
    

    我创建的 Codepen URL -http://codepen.io/nagasai/pen/EyKqMB 以供参考

    【讨论】:

    • 感谢您的回复 Naga Sai。但由于某种原因,我的范围不起作用。这是正确的做法吗?
    • 是的 Jithin,尝试将您的响应数据或 JSON 对象分配给机构变量,我提到的代码将为您工作
    • 我试过你的代码,但没有运气。问题是它是否从 URL localhost:8080/EtbosAdmin/agencies 获取数据。它不是从链接动态工作的吗?
    • 如果您从本地主机 URL 尝试,您将在开发者控制台中收到 CORS 错误....点击此链接可能对您有帮助 - stackoverflow.com/questions/31612931/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多