我需要在控制器的模板html文件中填充select(option)元素。我可以成功完成此操作,但不能提供默认值(避免使用第一个空选项)
template.html文件
...
<select name="drill_down" id="drill_down" ng-model="drill_down"
ng-options="item.value for item in items"></select>
...
controller.js文件
(function () {
'use strict';
var app = angular.module('appname', []);
app.config(function($stateProvider) {
$stateProvider
.state('appname', {
url: '/appname',
templateUrl: '/appname/template.html',
controller: 'AppCtrl'
});
});
app.controller('AppCtrl', AppCtrl);
function AppCtrl($scope, MyService) {
$scope.college_majors_full = [];
$scope.job_title_functions = [];
MyService.getData().then(function(rows) {
$scope.items = rows; // this works - populates select options
$scope.drill_down = $scope.items[0].value; // this doesn't work
});
...
有什么帮助吗?谢谢。
您的选项只有项目的文本属性
您正在分配值而不是ID
这样尝试
$scope.drill_down = $scope.items[0].value;
或者您可以在选项中创建值文本类型
这样尝试
看法
<select name="drill_down" id="drill_down" ng-model="drill_down"
ng-options="item.id as item.value for item in items"></select>
控制器
$scope.drill_down = $scope.items[0].id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句