无法从控制器刷新ng模型值和变量?

时间:2017-04-10 09:52:21

标签: javascript angularjs angularjs-scope angularjs-ng-model

我的视图部分有一些角度变量,想要在控制器实时更改其值时进行更新。 pager是我的模型,每当调用setpage()函数时,它都会被控制器更新。我在这里发布我的代码

HTML

<div class="col-sm-5 hidden-xs">
   <div class="dataTables_info" ng-model = "pager" id="example-datatables2_info" role="status" aria-live="polite">
      <strong>{{pager.startIndex}}</strong>-<strong>{{pager.endIndex + 1}}</strong> of <strong>{{list.count}}</strong>
   </div>
</div>

控制器

app.controller('listController',function($scope,$http,PagerService){

    $scope.list = {};
    $scope.pager = {};
    $scope.setPage = setPage;
    $scope.list = response;
    $scope.pageSize = "10";

    initController();

    function initController() {
        $scope.setPage(1);
    }

    function setPage(page,pageSize) {
        if (page < 1 || page > $scope.pager.totalPages) {
            return;
        }
        $scope.pager = PagerService.GetPager($scope.list.count, page,pageSize);
        if($scope.default != 1){

            var data = {"session_id":session_id, "offset":$scope.pager.startIndex, "limit":$scope.pager.pageSize};

            apiCall(data,"reportRoute.php?action=select&id="+id).then(function (res) {
                $scope.list.data = res.data.data;
                $scope.list.count = res.data.count;
                $scope.list.default = res.data.default;

            });
        }

    }


    function apiCall(postData, postUrl){
        postUrl = 'https://example.com/routes/'+postUrl;
        //getData = {action:validate,id:id};
        return $http({
            method  : 'POST',
            url     : postUrl,
            data    : postData,
            headers : {'Content-Type': 'application/json'},
            //params  : getData
        })
    }

})
第一次

寻呼机对象

{totalItems: 33287, currentPage: 1, pageSize: 10, totalPages: 3329, startPage: 1,startIndex:0,endIndex:9}
单击分页的第二页

时,

寻呼机对象

{totalItems: 33287, currentPage: 1, pageSize: 10, totalPages: 3329, startPage: 1,startIndex:10,endIndex:19}

所以在第二次点击时,角度变量不会实时更新。

1 个答案:

答案 0 :(得分:0)

app.controller('listController',function($scope,$http,PagerService,$timeout){

    $scope.list = {};
    $scope.pager = {};
    $scope.setPage = setPage;
    $scope.list = response;
    $scope.pageSize = "10";

    initController();

    function initController() {
        $scope.setPage(1);
    }

    function setPage(page,pageSize) {
        if (page < 1 || page > $scope.pager.totalPages) {
            return;
        }
        $scope.pager = PagerService.GetPager($scope.list.count, page,pageSize);
        if($scope.default != 1){

            var data = {"session_id":session_id, "offset":$scope.pager.startIndex, "limit":$scope.pager.pageSize};

            apiCall(data,"reportRoute.php?action=select&id="+id).then(function (res) {
 $timeout(function(){
  $scope.list.data = res.data.data;
  $scope.list.count = res.data.count;
  $scope.list.default = res.data.default;
  $scope.pager.startIndex = data.offset;
 $scope.pager.pageSize = data.limit;
 },0)


            });
        }

    }


    function apiCall(postData, postUrl){
        postUrl = 'https://example.com/routes/'+postUrl;
        //getData = {action:validate,id:id};
        return $http({
            method  : 'POST',
            url     : postUrl,
            data    : postData,
            headers : {'Content-Type': 'application/json'},
            //params  : getData
        })
    }

})