私は AngularJS が初めてで、選択ボックスにデフォルト値を設定しようとしています。選択ボックスにリストしたいオブジェクトのリストがあります。これは正常に機能し、選択ボックスに関連付けられたモデルへのバインディングは正常に機能します。ただし、デフォルト値を設定するとすぐに、すべてのバインディングが何らかの理由で更新されなくなります。
コントローラ:
app.controller('ctrl', [ '$scope', function ctrl($scope){
api.get('dealerships', function(err, dealerships){
$scope.dealerships = dealerships;
$scope.$apply();
});
});
HTML:
<select ng-model="dealership" ng-options="d.name for d in dealerships"></select>
<span class="dealership-name">{{dealership.name}}</span>
これは問題なく動作し、ディーラーを切り替えることができ、dealership.nameバインディングは問題なく更新されます。ただし、次のようにデフォルト値を設定するとすぐに:
app.controller('ctrl', [ '$scope', function ctrl($scope){
api.get('dealerships', function(err, dealerships){
$scope.dealerships = dealerships;
$scope.dealership = $scope.dealerships[0];
$scope.$apply();
});
});
私のディーラーとの拘束力はすべて同じままです。彼らはディーラーにとどまります[0]。
画像で理解しやすいかもしれないので:
誰にも手がかりはありますか?どんな助けでも大歓迎です。