首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ng-selected不在选择字段中工作

ng-selected不在选择字段中工作
EN

Stack Overflow用户
提问于 2016-09-02 21:24:15
回答 2查看 2.7K关注 0票数 0

我试图将select字段的默认值设置为set选项,但它不起作用,而且我似乎找不出我做错了什么?

在我的控制器中,我有:

代码语言:javascript
复制
$scope.interp_id = "2"; // set the default id
$http.get("web/controllers/get.php")
.then(function (response) {
    $scope.interpreters = response.data.records;
});

get.php是一个返回json的php文件(它工作正常)。

我的select如下所示:

代码语言:javascript
复制
<select ng-model="interpreters_id">
    <option ng-repeat="x in interpreters" value="{{x.id}}" ng-selected="{{x.id == interp_id}}">{{x.first_name}} {{x.middle_name}} {{x.last_name}}</option>
</select>

当我在浏览器中查看它时,它看起来是这样的:

但是当我检查该元素时,它显示选项ng-selected为True。

我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-02 22:13:14

ngOptions是在Angular中使用select元素的推荐方式。

此外,如果您查看ngSelected文档,会看到一个黄色的框,说明它不能正确处理select和ngModel属性,在某种程度上,这正是您要尝试做的事情。

ngRepeat将很乐意重复您的选项元素,但是这并不是使用select元素的正确方式。

我有prepared a fiddle可以让你试试。

代码语言:javascript
复制
<div ng-app="ngOptionsApp">
    <div ng-controller="ExampleController">
        <select ng-model="selectedInterpreter" ng-options="interpreter as interpreter.name for interpreter in interpreters track by interpreter.id">
            <option value="">Select an Interpreter</option>
        </select>  

        {{selectedInterpreter}}
    </div>
</div>

angular.module('ngOptionsApp', []).controller('ExampleController', ['$scope', function($scope) {
    $scope.interpreters=[{id: 1, name:'Gill Bates'}, {id: 2, name:'Cohn Jarmack'}, {id: 3, name:'Melon Musk'}];
    $scope.selectedInterpreter = $scope.interpreters[0];
}]);

希望它能有所帮助;)

票数 2
EN

Stack Overflow用户

发布于 2016-09-02 21:32:22

您不应该在ng-selected指令中使用大括号。你可以试试这个吗?

代码语言:javascript
复制
<option ng-repeat="x in interpreters" value="{{x.id}}" ng-selected="x.id == interp_id">{{x.first_name}} {{x.middle_name}} {{x.last_name}}</option>

http://jsfiddle.net/endrcn/44j19ngp/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39293565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档