Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用AngularJS中的名称和选定值从数组中选择选项_Html_Angularjs_Select_Selected - Fatal编程技术网

Html 使用AngularJS中的名称和选定值从数组中选择选项

Html 使用AngularJS中的名称和选定值从数组中选择选项,html,angularjs,select,selected,Html,Angularjs,Select,Selected,在我的项目中,我使用AngularJS作为从阵列接收多个数据的模板 json数组具有以下格式: {餐厅:{ 餐厅:{ id:1, 货币:3 }}} 变量是{{restaurant.restaurant.restaurant.money} 我需要得到下面描述的结果: <select> <option value="1">Meno di 15 euro</option> <option value="2">15-25 euro</

在我的项目中,我使用AngularJS作为从阵列接收多个数据的模板

json数组具有以下格式:

{餐厅:{ 餐厅:{ id:1, 货币:3 }}}

变量是{{restaurant.restaurant.restaurant.money}

我需要得到下面描述的结果:

<select>
    <option value="1">Meno di 15 euro</option>
    <option value="2">15-25 euro</option>
    <option value="3" selected="selected">25-45 euro</option>
    <option value="4">45-65 euro</option>
    <option value="5">Più di 65 euro</option>
</select>
其中选择选项value=3,因为它是从数组中提取的值

谢谢你的支持

编辑:这是我的

<select>
    <option value="1" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 1]}}">Meno di 15 euro</option>
    <option value="2" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 2]}}">15-25 euro</option>
    <option value="3" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 3]}}">25-45 euro</option>
    <option value="4" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 4]}}">45-65 euro</option>
    <option value="5" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 5]}}">Più di 65 euro</option>
</select>
这段代码是这样一行的

<select>
    <option ng-selected="" value="1">Meno di 15 euro</option>
    <option ng-selected="" value="2">15-25 euro</option>
    <option ng-selected="selected" value="3">25-45 euro</option>
    <option ng-selected="" value="4">45-65 euro</option>
    <option ng-selected="" value="5">Più di 65 euro</option>
</select>
但在浏览器中,重点是第一个选项值=1

编辑2:


这是我的

使用Ng选项而不是选项。并提供ng模型以选择默认值。。
<select>
    <option value="1" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 1]}}">Meno di 15 euro</option>
    <option value="2" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 2]}}">15-25 euro</option>
    <option value="3" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 3]}}">25-45 euro</option>
    <option value="4" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 4]}}">45-65 euro</option>
    <option value="5" ng-selected="{{{true: 'selected', false: ''}[restaurant.restaurant.Restaurant.money == 5]}}">Più di 65 euro</option>
</select>

请参见本页给出的示例。

您的选择可以是-

<select class="smallPageSizeInput" ng-model="selectedAmount" ng-options="selectedAmount for selectedAmount in ['Meno di 15 euro', '15-25 euro', '25-45 euro', '45-65 euro', 'Più di 65 euro']" ng-init="selectedAmount=userSelectedAmount"></select>
您需要更改resolvedUserObj以从阵列或从其他任何地方获取它


希望这有帮助。

在Angular中,您不需要手动管理所选的元素等。设置数据时,它将自动反映在HTML UI中

但是,根据您的评论,您似乎希望以非常具体的方式在HTML中执行某些操作。因此,您可以使用以下选项:

HTML将是:

    <select>
        <option value="1" ng-selected="restaurant.restaurant.Restaurant.money == '1'">Meno di 15 euro</option>
        <option value="2" ng-selected="restaurant.restaurant.Restaurant.money == '2'">15-25 euro</option>
        <option value="3" ng-selected="restaurant.restaurant.Restaurant.money == '3'">25-45 euro</option>
        <option value="4" ng-selected="restaurant.restaurant.Restaurant.money == '4'">45-65 euro</option>
        <option value="5" ng-selected="restaurant.restaurant.Restaurant.money == '5'">Più di 65 euro</option>
    </select>

请参见

请分享您迄今为止的尝试。@oibaf80您是指像这样的{餐厅:[{…,金钱:3,…}],…}您能提出或表明您的问题吗?传递到所选ng的表达式的计算结果应为true或false。顺便说一句,我已添加了JSFIDDLE。对不起,我无法修改控制器。对不起,我无法修改控制器。确定。那很有趣。在任何情况下,我都更新了解决方案以不更改控制器代码。以前,Angular不喜欢ng选项中的回车。现在应该与jsfiddle一起工作感谢您的建设性回答,但问题是,从控制器,我只收到选定的ID,也没有列表和名称。名称“Meno di 15欧元”,ecc。。。都是用html编写的。我希望能够将我从controller.OK收到的ID为3的选项设置为“selected=selected”。更新了解决方案,使其符合您的想法。如果您使用的是Angular,则不理想,但它会起作用。