如何让Angularjs中的控制器刷新?

[英]How can I get a controller to refresh in Angularjs?


I'm using Ionic Framework for an app and am sticking on one part. In my app I have a Favourites view, which displays a list of items that a user has favourited elsewhere in the app. The problem is, the code in the controller is only run once the first time the Favourites route is hit. If a user then adds a favourite somewhere else in the app, then goes back to the Favourites view, the list isn't regenerated.

我正在为一个应用程序使用Ionic Framework,并坚持一个部分。在我的应用程序中,我有一个收藏夹视图,其中显示了用户在应用程序中其他地方喜欢的项目列表。问题是,控制器中的代码仅在第一次点击收藏夹路由时运行。如果用户然后在应用程序中的其他位置添加收藏夹,然后返回“收藏夹”视图,则不会重新生成列表。

In my controller, I have the following:

在我的控制器中,我有以下内容:

Favourites.all().then(function(results){
    angular.forEach(results, function(value, key){
        // add results to an array to present in the view
    })
})

I have a Factory that does the database query:

我有一个工厂,它执行数据库查询:

.factory('Favourites', function(DB){
    var self=this;
    console.log("test 1");

    self.all = function() {
        return DB.query('SELECT * FROM favourites')
            .then(function(result){
                return DB.fetchAll(result);
            });
    };
    return self;
})

My question is, how can I get the Favourites controller to refresh after a user has added a new favourite to the database? I tried adding a scope reload function to the controller, and also adding reload:true to the $stateProvider for the route, but neither made any difference.

我的问题是,在用户将新收藏夹添加到数据库后,如何让收藏夹控制器刷新?我尝试将范围重新加载函数添加到控制器,并且还将reload:true添加到路由的$ stateProvider,但两者都没有任何区别。

2 个解决方案

#1


10  

Use broadcasting from anywhere and catch it wherever:

从任何地方使用广播并在以下任何地方捕获:

Anywhere in your app:

您应用中的任何位置:

$scope.onNewFavoriteHandler = function() {
    //Add to DB logic
    $rootScope.$broadcast('new-favorite');
}

On the relevant controller:

在相关的控制器上:

$scope.$on('new-favorite', function(event, args) { 
    Favourites.all().then(function(results){
        angular.forEach(results, function(value, key){
            // add results to an array to present in the view
        })
    });
    // do what you want to do
})

Nice artice if you wish to extend your knoledge

如果你想延长你的知识,那就很好了

#2


1  

you can use ui-router and resolve/reload() to get your controller/view to reload:

你可以使用ui-router和resolve / reload()来重新加载你的控制器/视图:

  .state('somestate', {
    url: '/somestate',
    templateUrl: '/path/to/some/view.html',
    resolve: {
      favorites: ['Favourites', function ($Favourites) {
        return Favourites.all();
      }]
    },
    controller: ['$scope', '$state', 'favorites', function ($scope, $state, favorites) {
      angular.forEach(favorites, function(value, key){...});

      //...later...
      $scope.onSomethingHappened = function reload(){
            var current = $state.current;
            var params = angular.copy($stateParams);
            $state.transitionTo(current, params, { reload: true, inherit: true, notify: true });          
    }]
  })
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/12/15/d31568de6e868c18cb096672d7a157ee.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告