AngularJS How do I select a dropdown value when the value is retrieved from a web service?

Hi, the community has been stuck on this for a while ...

Scenario: I have a drop-down list that I pre-populate with the values ​​received from the service. Now the same object returned from the service allows me to find out the value that must be pre-selected for this drop-down list. The service returns a JSON object with all this information.

JSON Example Return from Service

{
    "stepsInvolved": [{
        "label": "Step 1",
        "value": "Step 1"
    }, {
        "label": "Step 1",
        "value": "Step 1"
    }, {
        "label": "Step 1",
        "value": "Step 1"
    }],
    "valueSelected": {
        "label": "Step 1",
        "value": "Step 1"
    }
}

Question

Using Angular I assign the values ​​of a drop-down list from stepsInvolvedto a variable region $scope.options, which is pretty straight forward.

, "valueSelected" JSON? angular?

JSFiddles Plunkers .

P.S: ,

$scope.options= stepsInvolved;
$scope.selectedOption = $scope.options[1];
+4
2

select

asign

$scope.selectedValue = 'Step 1';  //your selected value, assign it in any way you wish
$scope.stepsInvolved = response['stepsInvolved'];

, ""

<select ng-model='selectedValue' ng-options="item.value as item.label for item in stepsInvolved"></select>
+3

Source: https://habr.com/ru/post/1533106/


All Articles