简体   繁体   English

Angular / Ionic回到状态

[英]Angular/Ionic going back to the state

I have an article list page where I have filters for articles, latest and popular. 我有一个文章列表页面,其中有用于最新和热门文章的过滤器。 Their buttons look like that in the front page: 他们的按钮看起来像首页中的那样:

<a class="button button-icon" href="#" ng-click="articleFilter('latest')"><i class="ion-ios-circle-filled" ng-show="bulletpointLatest"></i> Latest
      </a>
<a class="button button-icon" href="#" ng-click="articleFilter()"><i class="ion-ios-circle-filled" ng-show="bulletpointPopular"></i> Popular
      </a>

I have set up the filters in my FrontPageController that looks like this: 我已经在FrontPageController中设置了如下所示的过滤器:

  .controller('FrontPageController', function($scope, ArticleService, $state, $ionicScrollDelegate, $location, $ionicPosition) {
  ArticleService.all().then(function(data){
    $scope.articles = data;
  });

  $scope.$on('$ionicParentView.afterEnter', function(event, data) {
    if (data.direction == 'back') {
      $scope.doRefresh();
    }
  });

  $scope.bulletpointSiste = true;

  $scope.articleFilter = function(button){
    if (button == "siste"){
      $scope.bulletpointSiste = true;
      $scope.bulletpointPopular = false;
      ArticleService.all().then(function(data){
        $scope.articles = data;
      });
    }
    else {
      $scope.bulletpointSiste = false;
      $scope.bulletpointPopular = true;
      ArticleService.popular().then(function(data){
        $scope.articles = data;
        console.log(data);
      });
    }
  };

  $scope.like = function(article){
    article.like = article.like == 0 ? 1 : 0;
    ArticleService.like(article.id)
  };

  $scope.doRefresh = function (){
    ArticleService.all().then(function(data){
      $scope.articles = data;
    })
    .finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });
  };
})

My config for the front page route is: 我的首页路由配置为:

.state('main.front', {
    url: '/front',
    views: {
      'content': {
        templateUrl: 'templates/main-front.html',
        controller: 'FrontPageController'
      }
    },
    authenticate: true
  })

In my article page view I have tried to go back to the front page by using ng-click. 在我的文章页面视图中,我尝试使用ng-click返回首页。

<a ng-click="goBack()" nav-direction="back">

And in my article controller I tried with ionic history: 在我的文章控制器中,我尝试了离子历史记录:

$scope.goBack = function(){
    $ionicHistory.goBack()
  };

Update 更新资料

Now, since I am refreshing articles on back with: 现在,由于我正在刷新以下文章:

$scope.$on('$ionicParentView.afterEnter', function(event, data) {
    if (data.direction == 'back') {
      $scope.doRefresh();
    }
  });

I need that in order to change the style for like icons if they were clicked in an article page that they get an appropriate style for that. 如果需要在文章页面中单击类似图标的样式,则需要更改样式,以便更改样式。 Since doRefresh() function gets all the latest articles: 由于doRefresh()函数获取所有最新文章:

$scope.doRefresh = function (){
    ArticleService.all().then(function(data){
      $scope.articles = data;
    })
    .finally(function() {
       $scope.$broadcast('scroll.refreshComplete');
     });
  };

I need to somehow make it work so that I know what was the previous state so that I can call appropriate service on refresh, popular if the user is coming from an article that he opened on the list of popular articles. 我需要以某种方式使其工作,以便我知道以前的状态,以便可以在刷新时调用适当的服务,如果用户来自他在热门文章列表中打开的文章,则该服务很受欢迎。 How should I do that? 我应该怎么做?

@Marco: Can you change your goBack function to the following code with input the front view state name. @Marco:您可以通过输入前视图状态名称将goBack函数更改为以下代码吗?

var goBack = function() {
   $state.go('VIEW_NAME');//
}

Because my experience is the view will always be cached unless we disable it. 因为我的经验是,除非我们禁用它,否则视图将始终被缓存。

Refer here 请参考这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM