我需要从 Controller 填充模板 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>
Controller
$scope.drill_down = $scope.items[0].id;
关于javascript - 从 Controller 访问 templateUrl 内的元素 - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32145070/