使用angularjs设置选择框的默认项

使用angularjs设置选择框的默认项,angularjs,Angularjs,我正在尝试使用angularjs设置加载时选择框的默认项 我从2个json中加载两个选择框,因此第二个选择框名为“城市”,依赖于第一个选择框“国家”: <label>Country:</label> <select name="country" ng-model="form.country" ng-options="c.n for c in countryList" ng-change="countryChanged()"

我正在尝试使用angularjs设置加载时选择框的默认项

我从2个json中加载两个选择框,因此第二个选择框名为“城市”,依赖于第一个选择框“国家”:

  <label>Country:</label>
  <select name="country" ng-model="form.country" 
        ng-options="c.n for c in countryList" 
        ng-change="countryChanged()" required></select>

  <label>City:</label>
  <select name="city" ng-model="form.city" 
        ng-options="c for c in cityList" required></select>
控制器:

$http.get('countries.json')
  .success(function (data) {
    $scope.countryList = data;
    $scope.form.country = data[0];
    // $scope.form.country.c = 'fr'; <<<--- *trying to set default*
    $scope.countryChanged();
});

$scope.countryChanged = function() {

  $http.get($scope.form.country.c + '-cities.json')
    .success(function (data) {
      $scope.cityList = data;
      $scope.form.city = data[0];
    });
}
$http.get('countries.json')
.成功(功能(数据){
$scope.countryList=数据;
$scope.form.country=数据[0];

//$scope.form.country.c='fr';不确定,但这是否满足要求?

var selectCountry=函数(数据、代码){
对于(变量i=0;i
不确定,但这是否满足要求?

var selectCountry=函数(数据、代码){
对于(变量i=0;i
只需在控制器的第12行设置:

$scope.form.country = data[1];

它将法国设置为默认值,希望我理解您的问题。

只需在控制器的第12行设置:

$scope.form.country = data[1];

它将法国设置为默认值,希望我理解你的问题。

谢谢。这与我所追求的差不多。尽管我认为会有一种“angularjs”的方法来实现。angularjs需要一些json查询功能(coughlinq);)谢谢。这正是我想要的。虽然我认为会有一种“angularjs”的方法来做。angularjs需要一些json查询的东西(coughlinq);)
$scope.form.country = data[1];