読み込み時に国のリストを表示するディレクティブを作成しました。ユーザーが再びページにアクセスしたときに、選択した国を「記憶」する必要があります (SPA 内を移動した後)。選択した国をサービスに保存しています。ページが再度読み込まれると、サービスから国を取得し、ディレクティブ内の選択ボックスのモデルにバインドされている「scope.country」変数に保存します.
テンプレート:
<select ng-model="country" ng-options="country.Name for country in data.countries" class="form-control">
<option value="">Country</option>
</select>
指令:
.directive('ggCountry', ["$http", function ($http) {
return {
templateUrl: '../../Common/js/angular/directives/templates/CountryDropDown.html',
scope: {
country: '=ngModel'
},
link: function (scope, element, attrs) {
scope.data = {};
scope.data.countries = [];
$http.post("../Common/GetAllCountries", {}).then(function (answer) {
scope.data.countries = answer.data.d;
// select the country:
if (scope.country != undefined) {
// using underscore library for the selection
scope.country = _.where(scope.data.countries, { Id: scope.country.Id });
}
});
}
};
}])
国を選択して選択ドロップダウンリストのモデルとして設定した後、ビューが変更され、この国が選択されると予想されますが、何らかの理由でそのように動作しません。