2014-09-30 69 views
0

這是我的問題:我需要使用API​​調用來拉出時區的列表來填充頁面上的select元素。什麼是適當的angularjs方式呢?我已經試過:如何更新在angularjs數組更改選擇?

HTML:

<select class="custom-select timezone" name="Timezone" id="Timezone" ng-model="regCtrl.user.Timezone" ng-options="timezone.DisplayName for timezone in regCtrl.Timezones></select> 

的JavaScript:

app.controller('RegisterController', ['$http', '$scope', function ($http, $scope) { 
    $scope.user = {}; 
    $scope.Timezones = []; 
    $http.get('/User/GetTimezones').success(function (result) { 
     $scope.Timezones = result; 
    }); 
}); 

我在做什麼錯?

編輯: 它不是this question的重複 - 我試過了,它沒有幫助。該錯誤必須以某種方式在語法上。如果我不正確,並且這是上述問題的重複,有人可以通過示例告訴我如何將該修復應用於我的代碼。謝謝。

+0

能不能請你聯繫我的問題的重複,因爲如果你認爲這[鏈接]的(http://stackoverflow.com/questions/19872387/in -angularjs-how-do-you-get-a-select-to-refresh-when-the-array-for-ng-options?lq = 1) - 我試過了,它不適用於我的問題。 – 2014-09-30 07:00:31

回答

0

只需將控制器名稱在你html的

<select class="custom-select timezone" name="Timezone" id="Timezone" ng-model="user.Timezone" ng-options="timezone.DisplayName for timezone in Timezones></select> 
+0

而不是僅僅將代碼傾倒在答案中,你會不會解釋你改變了什麼,以及爲什麼它應該起作用? – Cerbrus 2014-09-30 06:54:11

+0

準確地說 - 您已經更改了ng-model,該模型在開始時工作正常。我遇到的問題是通過使用回調函數更新數組而導致的,並且html在此之後不會自動更新。當我在開始時設置列表時,它一切正常。 – 2014-09-30 06:55:55

相關問題