我希望当我在视图中移动并返回时,选择中的选项保持选中状态。
这是视图:
<select ng-model="getseason" class="form-control" ng-options="season as 'Season '+ season + '/' + seasonaddone(season) for season in seasons">
这是控制器:
$scope.seasons = [2014,2013,2012,2011,2010,2009,2008,2007,2006,2005];
$scope.getseason = $scope.seasons[0];
$scope.$watch('getseason',function(){
var url = 'http://api.football-data.org/alpha/soccerseasons/?season=' + $scope.getseason;
apiDataService.GetData(url).then( function( data) {
$scope.leagues = data;
});
});
我知道从其他视图返回时,此控制器会再次加载并将值设置为
season[0]
。 最佳答案
创建一个服务并将此值存储在那里。每个应用程序加载时每个服务仅被实例化一次,因此,如果在其中存储一个值,则在路由更改后它不会消失。顺便说一句。这也是存储模型(例如,季节数组)的好地方。实现示例:
app.controller('MyController', function ($scope, seasonHelper) {
$scope.getseason = seasonHelper.getseason;
$scope.$watch('getseason', function(){
seasonHelper.getseason = $scope.getseason;
...
});
})
.service('seasonHelper', function () {
this.seasons = [2014,2013,2012,2011,2010,2009,2008,2007,2006,2005];
this.getseason = this.seasons[0];
});