2015-01-13 40 views
0

我想填充使用角度的HTML選擇。我測試的API和得到的東西,如:填充選擇不觸發角度控制器

{"Countries":[{"Id":1,"Name":"Andorra"},{"Id":2,"Name":"France"}]} 

的HTML如下:

<form id="form" method="post" ng-submit="submit"> 
    <select ng-model="model.currentCountry" ng-options="country.Id as country.Name for country in model.countries" ng-controller="CountryController"> 
    <option value="">Country</option> 
    </select> 
</form> 

然後,我有JS代碼:

var application = angular.module('Application', []); 

application.service('CountryService', function ($http) { 
    return { 
    GetList: function() { 
     return $http.get('api/countries'); 
    } 
    } 
}); 

application.controller('CountryController', function CountryController($scope, CountryService) { 

    alert("Country Controller"); 

    $scope.model = { 
    currentCountry: '', 
    countries: [] 
    } 

    CountryService.GetList() 
    .success(function (data, status, headers, config) { 
     $scope.model.countries = $scope.model.countries.concat(data) 
    }) 
    .error(function (data, status, headers, config) { }); 

}); 

不知怎的,甚至沒有在CountryController警報被解僱。

任何想法我錯過了什麼?

+0

您可以提供的代碼/標記被指定控制器玩?無論這是一條路線還是您正在使用ng-controller。 – Brocco

+0

我正在使用ng-controller。查看我的HTML選擇代碼。最後你有ng控制器。 –

+1

啊,我錯過了(沒有向右滾動)。我假設你已經通過ng-app =「Application」在dom中進一步定義了ng-app? – Brocco

回答

1

在你的控制器,你有成功處理程序....

$scope.model.countries = $scope.model.countries.concat(data) 

那麼,你是從你的例子JSON concatting國家對象,更改此:

$scope.model.countries = $scope.model.countries.concat(data.Countries) 

這樣您將實際的國家數組連接到您的model.countries。

編輯這裏有一個例子plnkr

+0

謝謝,解決了它......而且我也錯過了ng-app,因爲我將它添加到未包裝div的div上。我把它移到了HTML標籤。 –