0
我需要從控制器中填充模板html文件中的選擇(選項)元素。我能做到這一點成功,但不能放棄默認值(以避免第一空選項)來自控制器的templateUrl中的訪問元素 - AngularJS
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
});
...
任何幫助嗎?謝謝。
感謝您的幫助!有用。但現在它選擇最後一個選項作爲默認值。 即使我選擇另一個選項,總是隻選擇最後一個選項。 – Alex
你能提供一個小提琴嗎? –
我自己解決了。問題是我沒有正確寫入數據結構。 JSON數據結構:items = [{name:'xxx',value:'first'},...] 但是我用 ... ng-options =「item.id as item.value for item in items ... 我應該寫名字而不是id 謝謝!! – Alex