【问题标题】:AngularJS Typeahead error on returning results返回结果时的AngularJS Typeahead错误
【发布时间】:2015-02-26 21:21:20
【问题描述】:

我有以下使用 AngularJS 和 Angular Bootstrap Typeahead 小部件的代码。问题是它只返回一个错误属性长度“未定义”。任何帮助将不胜感激。

HTML

<h4>Asynchronous results</h4>
 <pre>Model: {{asyncSelected | json}}</pre>
  <input type="text" ng-model="asyncSelected" placeholder="Patients loaded via $http" typeahead="result.patient.drug.drugindication for result in getPatient($viewValue)" typeahead-loading="loadingPatients" class="form-control">
 <i ng-show="loadingPatients" class="glyphicon glyphicon-refresh"></i>

Javascript

$scope.getPatient = function(val) {
return $http.get('https://api.fda.gov/drug/event.json', {
  params: {
    search: 'patient.drug.drugindication:' + val
  }
}).then(function(response){
    //the following console log returns the data just fine
    console.log(response.data.results)
  return 
  { 
    result: response.data.result
  }
});
};

错误:

TypeError: 无法读取未定义的属性“长度”

【问题讨论】:

    标签: angularjs angularjs-scope angular-ui-bootstrap bootstrap-typeahead angular-ui-typeahead


    【解决方案1】:

    这意味着您的服务器的响应不包含请求的数据,如果您输入浏览器地址

    https://api.fda.gov/drug/event.json?patient.drug.drugindication=a

    你只能看到错误信息

    【讨论】:

      【解决方案2】:

      首先,查看此链接https://api.fda.gov/drug/event.json?search=patient.drug.drugindication:ASPIRIN

      那么你必须把你的代码改成这个,所以你只需要返回 Promise 并且你不需要使用 then() 所以把你的代码改成

           $scope.getPatient = function(val) {
            return $http.get('https://api.fda.gov/drug/event.json', {
             params: {
                 search: 'patient.drug.drugindication:' + val
              }
            })
           };
      

      如果你想使用 then() ,请遵循此代码,这意味着返回数据

      $scope.getPatient = function(val) {
      var data = $http.get('https://api.fda.gov/drug/event.json', {
        params: {
          search: 'patient.drug.drugindication:' + val
        }
      });
      data.then(function(response){
         console.log(response.data.results)
      });
        return data;
      };
      

      如果您想了解更多,请遵循以下模式

      https://api.fda.gov/drug/event.json?search=seriousnesslifethreatening:1+AND+patient.drug.medicinalproduct:%22ASPIRIN%22+AND+patient.patientsex:1&limit=10
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多