如何在角度JS中刷新视图?

时间:2014-08-10 09:55:30

标签: javascript jquery angularjs

在Web应用程序中,我有一个属于一个或多个帐户的用户,并且在每个帐户中都存在用户更改其帐户时需要获取属于该帐户的新交易的交易。我所做的是制作一个到目前为止,除了我必须点击刷新按钮以获取新数据(事务)之外,每个事情都能正常工作的指令。我的第一个想法是获取当前用户位置并将他重定向到同一位置以刷新数据,但这不起作用。

.directive("accountslist",  function(AccountService, Session, $location){
        return{
            restrict:'E',
            replace:'true',
            template:
            '<select ng-change="SetAcc()" ng-model="MyAcc" ng-options="Acc for Acc in userAccounts"></select>',
            controller: function($scope){
                $scope.userAccounts = AccountService.getAccounts();

                $scope.SetAcc = function(){
                    var userMail = Session.get_user_mail();
                    var setUser = AccountService.SetUserAccount($scope.MyAcc, userMail);
                    setUser.then(function(response){
                         //http call with new data
                         CategoryService.getCategories();
                        //here i need to updata the view
                    },
                    function(error){
                        console.log(error);
                    });
                };
            }
     };

    });

她是在类别控制器中设置类别的示例,我需要在更改帐户时执行相同的操作。

   $scope.getCategories =function(){
    $scope.categories.getCategories().success(function(data){
        $scope.categories.categories = data; 
        $scope.allCategoriesSum();
    })
    .error(function(data, status, headers){alert(status);}); 
}

1 个答案:

答案 0 :(得分:0)

假设您在AccountService中有一个函数“getTransactions()”从服务器检索帐户的所有事务,您可以这样做:

.directive("accountslist",  function(AccountService, Session, $location){
    return{
        restrict:'E',
        replace:'true',
        template:
        '<select ng-change="SetAcc()" ng-model="MyAcc" ng-options="Acc for Acc in userAccounts"></select>',
        controller: function($scope){
            $scope.userAccounts = AccountService.getAccounts();

            $scope.SetAcc = function(){
                var userMail = Session.get_user_mail();
                AccountService.SetUserAccount($scope.MyAcc, userMail)
                    .then(function (response) {
                        return AccountService.getTransactions($scope.MyAcc, userMail);
                    })
                    .then(function (transactions) {
                        $scope.transactions = transactions;
                    })
                    .catch(function(error){
                        console.log(error);
                    });                   
            };
        }
 };

});