我试图将select字段的默认值设置为set选项,但它不起作用,而且我似乎找不出我做错了什么?
在我的控制器中,我有:
$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如下所示:
<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。

我做错了什么?
发布于 2016-09-02 22:13:14
ngOptions是在Angular中使用select元素的推荐方式。
此外,如果您查看ngSelected文档,会看到一个黄色的框,说明它不能正确处理select和ngModel属性,在某种程度上,这正是您要尝试做的事情。
ngRepeat将很乐意重复您的选项元素,但是这并不是使用select元素的正确方式。
我有prepared a fiddle可以让你试试。
<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];
}]);希望它能有所帮助;)
发布于 2016-09-02 21:32:22
您不应该在ng-selected指令中使用大括号。你可以试试这个吗?
<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/
https://stackoverflow.com/questions/39293565
复制相似问题