移动浏览视图时,请记住角度选择

时间:2015-06-27 20:18:01

标签: javascript html json angularjs angularjs-directive

当我浏览视图并返回时,我希望选择中的选项保持选中状态。

以下是观点:

<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]

1 个答案:

答案 0 :(得分:1)

创建服务并在此处存储此值。 每个服务仅在每次应用程序加载时实例化,因此如果您在那里存储值,则在路由更改后它不会消失。顺便说一句。这也可以是存储模型的好地方(即季节数组)。示例实施:

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];

});