如何根据选择框值从API获取值?

时间:2017-12-05 06:54:40

标签: javascript angularjs

我有一个API来获取下拉列表和评分值的值。

API screenshot

1)我有一个UI,如下图所示。

UI screenshot

2)代码如下:

<td>
    <div class="">
        <select ng-if="question.MasterDataCategoryId != null" id="answer{{question.QuestionId}}" ng-model="question.MasterData" ng-init="question.MasterData;" class="form-control" style="width: 300px; white-space:pre-wrap;">
            <option selected="selected"></option>
            <option ng-repeat="answer in answerData[question.MasterDataCategoryId]" value="{{answer.MasterData}}">{{answer.MasterData}}</option>
        </select>
        <input ng-if="question.MasterDataCategoryId == null" id="{{question.QuestionId}}" ng-model="question.MasterData" type="text" style="width: 300px;" name="question.QuestionId" class="form-control">
    </div>
</td>
<td>
    <div class="">
        <span ng-if="question.MasterDataCategoryId != null">{{question.Score}}</span>
        <select ng-if="question.MasterDataCategoryId == null" id="projectlist" class="form-control" required ng-model="scoring">
            <option selected="selected"></option>
            <option ng-repeat="score in scoringDetails" value="{{score.Score}}">
                {{score.Score}}
            </option>
        </select>
    </div>
</td>

根据下拉值,我需要动态显示我将在同一API中获得的评分。

任何让我知道如何实现这一点。

2 个答案:

答案 0 :(得分:0)

您可以在所需的下拉列表中使用ng-change = "someFunction()"。使用http GETPOST方法在该函数中调用您的API,如下所示 -

$http.get(url)
    .then(function(response) {
        console.log(JSON.stringify(response))
        // set your scoring variable here
        $scope.scoring = yourValue;    // your value from response
    }).error(function(error) {
        console.log(JSON.stringify(error))
    });

答案 1 :(得分:0)

在ng-change of dropdown中写下一个改变得分下拉值的功能。假设以下代码

在HTML中

<select ng-change="scoreChange()"  ng-model="dropvalue">
  <option></option>.....
</select>

<select ng-model="score">
  <option></option>.....
</select>

在控制器

$scope.scoreChange = function(){
  $scope.score = desiredvalue; 
  //or you can call api and assign it here.
}
相关问题