角度 js 选择刷新后缺少的框项



我尝试设置下拉框显示默认项目。首先,下拉框中的项目能够正确显示,我可以选择该项目以保存到数据库中。一切正常,问题是在我刷新页面后,默认项目始终为空,它应该显示我保存的项目。

HTML
<select class="form-control" ng-model="sst_type" ng-options="type for type in 
sst_types" ng-change="changeSstType(sst_type)"></select>

.JS

.controller('sstCtrl', function ($scope, $rootScope, Module, settings, 
toastr, actionBar, $uibModal) {
// scope properties
$scope.loading = true;
$scope.sst_types = ['Sotong', 'Bento'];
$scope.sst_all = [];
// scope functions
$scope.getSst = function () {
Module.getSst().then(function (res) {
$scope.loading = false;
if (res.sst_all) {
angular.forEach(res.sst_all, function (v) {
$scope.sst_all.push({
id: v.id,
name: v.name,
value: v.value,
percentage: (v.value * 100).toString(),
type: v.value == 0 ? 'Sotong' : 'Bento'
});
});
}
});
};

您需要从服务器返回已保存项目的 id。你可以在 Module.getSst(( 调用中执行此操作。 在 foreach 中,将 $scope.sst_type 设置为匹配的 for。

祝你好运

最新更新