简体   繁体   English

不会调用$ watch,Angularjs

[英]$watch is not called, Angularjs

I have a problem with $scope.$watch call, when it obviously should be called. 我显然在应该调用$ scope。$ watch时遇到问题。 I have a paginator (bootstrap UI) inside my html document: 我的html文档中有一个分页器(引导UI):

<pagination  total-items="paginatorTotalItems" items-per-page="paginatorItemsPerPage" 
   page="paginatorCurrentPage" max-size="paginatorSize" class="pagination-sm" 
   boundary-links="true">
</pagination>

A certain part, where my items are shown (for them I need a paginator): 显示我的商品的某个部分(为此,我需要一个分页器):

<div ng-show="reviews" ng-repeat="review in reviewsPerPage">
...
</div>

And a Controller: 和一个控制器:

...
$scope.reviewsArray = [];
$scope.paginatorItemsPerPage = 1;
$scope.paginatorSize = 3;
$scope.reviewsPerPage = [];
$scope.paginatorTotalItems = $scope.reviews.result.total;

       //restangular object to Array
       for (var i = 0; i < $scope.paginatorTotalItems; i++) {
           $scope.reviewsArray.push($scope.reviews.result.reviews[i]);
       };

       $scope.paginatorCurrentPage = 1;

$scope.$watch('paginatorCurrentPage', function () {
               var begin = (($scope.paginatorCurrentPage - 1) * $scope.paginatorItemsPerPage);
               var end = begin + $scope.paginatorItemsPerPage;

               console.log($scope.paginatorCurrentPage);
               console.log(begin + ' ' + end);

               $scope.reviewsPerPage = $scope.reviewsArray.slice(begin,end);

               console.log($scope.reviewsPerPage);
});

So, making long story short, I have a variable paginatorCurrentPage, that I change by clicking numbers in my <pagination> , but $watch does not react. 因此,总而言之,我有一个变量paginatorCurrentPage,可以通过单击<pagination>数字进行更改,但$ watch不会响应。 This $watch is called only once: when I'm assigning it a value of 1 (after making an array from my restangular object), after that $watch is never called anymore. 这个$ watch仅被调用一次:当我为其分配值1(从我的矩形对象创建数组之后),此后便不再调用$ watch。 Also I'm cheking how paginatorCurrentPage changes in my html file: 另外,我正在检查paginatorCurrentPage如何在我的html文件中进行更改:

<p>Current : {{paginatorCurrentPage}}</p>

And it actually works, this variable is changing, when i switch my pagination buttons, but $watch is not called. 它确实有效,当我切换分页按钮时,此变量正在更改,但是未调用$ watch。

Sorry for my English, and Thank you! 对不起,我的英语,谢谢!

Edited : 编辑:

I have updated my bootstrap UI, so now in paginator I use ng-model istead of page . 我已经更新了我的引导用户界面,所以现在在分页器中我使用ng-model不是page And I realized that variable paginatorCurrentPage changes only in my view, but in controller I still have my default $scope.paginatorCurrentPage = 1 . 我意识到变量paginatorCurrentPage仅在我的视图中发生了变化,但是在控制器中,我仍然具有默认的$scope.paginatorCurrentPage = 1 Problem still exists. 问题仍然存在。

Thanks for all comments. 感谢所有评论。 The problem was about scope. 问题在于范围。 I rewrote ng-model in paginator: ng-model="paginatorPage.current" 我在分页器中重写了ng-model: ng-model="paginatorPage.current"

and changed 并改变了

$scope.paginatorCurrentPage = 1;

to

$scope.paginatorPage = {current : 1};

And thanks to @Leo Farmer for advice about dots in directives. 感谢@Leo Farmer对指令中点的建议。

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

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