AngularJS怎样pre选择下拉列表的值当值从Web服务中检索? [英] AngularJS How do I pre select the value of a dropdown when the value is retrieved from a webservice?
问题描述
您好一直停留在这一个一阵子社区...
情景:
我有一个DropDownList,我美元,从服务中获取的值P1 $ P-填充。
现在,从服务返回的同一对象让我知道,必须是pre-选定为DropDownList的值。该服务将返回一个JSON对象的所有信息。
样从JSON服务返回
{
stepsInvolved:[{
标签:第1步,
值:第1步
},{
标签:第1步,
值:第1步
},{
标签:第1步,
值:第1步
}],
valueSelected:{
标签:第1步,
值:第1步
}
}
问题
采用了棱角分明我指定从 stepsInvolved
下拉列表值转换为范围的变量 $ scope.options
这就是pretty直线前进。
的如何preSELECT从JSON对象中的valueSelected表示与该值DropDownList的价值?你怎么和角?做到这一点的
JSFiddles和Plunkers的欢迎。
P.S:显然,这并不对这种情况下的工作。
$ scope.options = stepsInvolved;
$ scope.selectedOption = $ scope.options [1];
使值在选择标签模型
在你的控制器ASIGN
$ scope.selectedValue =第1步; //你选择的值,在你希望的任何方式分配给它
$ scope.stepsInvolved =响应['stepsInvolved'];
我假设你的对象被分配到回应
<选择NG-模式='了selectedValue'NG选项=item.value作为item.label在stepsInvolved项GT&;其中p /选择>
Hello community been stuck on this one for a while...
Scenario: I have a dropdownlist which I pre-populate with values fetched from a service. Now that same object returned from the service lets me know the value that needs to be pre-selected for that dropdownlist. The service returns a JSON object with all this information.
Sample JSON Returned 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'm assigning the dropdown list values from stepsInvolved
into a scope variable $scope.options
thats pretty straight forward.
How do I preselect the value on the dropdownlist with the value indicated in the "valueSelected" from the JSON object? How do you accomplish this with angular?
JSFiddles and Plunkers are welcome.
P.S : Obviously this doesn't work for this scenario
$scope.options= stepsInvolved;
$scope.selectedOption = $scope.options[1];
Give value to your model in select tag
In your controller asign
$scope.selectedValue = 'Step 1'; //your selected value, assign it in any way you wish
$scope.stepsInvolved = response['stepsInvolved'];
I am assuming your object is assigned to "response"
<select ng-model='selectedValue' ng-options="item.value as item.label for item in stepsInvolved"></select>
这篇关于AngularJS怎样pre选择下拉列表的值当值从Web服务中检索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!