[英]Trigger function on a certain element - ngrepeat - angularjs
Good morning, 早上好,
I'm trying to change the limitTo filter on a certain list, my issue is: when I click to the trigger who change the filter limit the filter changes on all ng-repeated categories. 我正在尝试更改某个列表上的limitTo过滤器,我的问题是:当我单击触发器来更改过滤器限制时,该过滤器会在所有重复ng的类别上进行更改。
my function inside the main controller 我在主控制器中的功能
$scope.showMore = function(limit) {
if($scope.limitItems === $scope.itemsPerList) {
$scope.limitItems = limit;
$scope.switchFilterText = 'less';
} else {
$scope.switchFilterText = 'more';
$scope.limitItems = $scope.itemsPerList;
}
}
my scenario (I rewrote it in a simplified version) 我的情况(我将其重写为简化版本)
<li ng-repeat="item in category.items | limitTo: limitItems ">
{{item.title}}
</li>
<li ng-if="limitItems < (category.items.length)">
<a ng-click="showMore(category.items.length)" >Show {{ switchFilterText }}</a>
</li>
Could you explain me what's wrong with me? 你能解释一下我怎么了吗? I searched how to select a single element to apply the function but I didn't find anything useful 我搜索了如何选择单个元素以应用该功能,但没有发现任何有用的信息
Update : 更新 :
I found the way to solve my issue in this way: No functions inside the controller are involved to make this functionality works properly: 我找到了以这种方式解决我的问题的方法:控制器内部不涉及任何使该功能正常工作的功能:
<li ng-repeat="category in maincategories" ng-init="limitItems = maxItemsPerList">
{{category.title}}
<ul>
<li ng-repeat="item in category.items | limitTo: limitItems "> {{item.title}}
</li>
</ul>
<a ng-click="limitItems = category.items.length" href>
<b ng-if="category.items.length > maxItemsPerList && limitItems != category.items.length "> Show more </b>
</a>
I'm not really convinced about Angular (I used it in my past and I was impressed by the performance but now I can see logics senseless): 我对Angular并没有真正的信心(我过去曾经使用过它,性能给我留下了深刻的印象,但现在我发现逻辑毫无意义):
What I learned: 我学到的是:
ng-if and ng-click cannot be used in the same content because ng-if creates new scopes so if you put ng-if on top of the "show more" link it will break the code ng-if和ng-click不能在同一内容中使用,因为ng-if会创建新的作用域,因此,如果将ng-if放在“显示更多”链接的顶部,则会破坏代码
ng-init cannot be used in the same element of the ng-repeat otherwise the var initialised will not be available inside the ng-repeat block ng-init不能在ng-repeat的同一元素中使用,否则ng-repeat块内部将无法使用初始化的var
I think there is another way to do that, maybe more clean but in this specific case I can't do a lot. 我认为还有另一种方法可以做到这一点,但也许更干净,但是在这种情况下,我不能做很多事情。
ng-if and ng-click cannot be used in the same content because ng-if creates new scopes so if you put ng-if on top of the "show more" link it will break the code ng-if和ng-click不能在同一内容中使用,因为ng-if会创建新的作用域,因此,如果将ng-if放在“显示更多”链接的顶部,则会破坏代码
Yes, ng-if creates a new scope, but it is possible to mix ng-if and ng-click (and most other directives). 是的,ng-if创建了一个新的作用域,但是可以将ng-if和ng-click(以及大多数其他指令)混合使用。 To do that, you'll be safer if you always write to atributes of another object instead of a simple variable. 为此,如果您总是写另一个对象的属性而不是简单的变量,那将更加安全。 It is plain JavaScript prototypal inheritance in play. 它是纯JavaScript原型继承性。
<li ... ng-init="category.limitItems = maxItemsPerList">
ng-init cannot be used in the same element of the ng-repeat otherwise the var initialised will not be available inside the ng-repeat block ng-init不能在ng-repeat的同一元素中使用,否则ng-repeat块内部将无法使用初始化的var
True, in the sense that variables are created in the local scope. 是的,从某种意义上说,变量是在本地范围内创建的。 But again, refer to an object. 但是同样,引用一个对象。
I think there is another way to do that, maybe more clean but in this specific case I can't do a lot. 我认为还有另一种方法可以做到这一点,但也许更干净,但是在这种情况下,我不能做很多事情。
You don't need to do a lot, it is quite simple to do it right actually. 您不需要做很多事情,实际上正确地做起来很简单。
Some advices: 一些建议:
Full code goes like this: 完整的代码如下所示:
<li ng-repeat="category in maincategories" ng-init="category.limitItems = maxItemsPerList">
{{category.title}}
<ul>
<li ng-repeat="item in category.items | limitTo: category.limitItems "> {{item.title}}
</li>
</ul>
<a ng-if="category.items.length > maxItemsPerList && category.limitItems != category.items.length" ng-click="category.limitItems = category.items.length" href>
<b> Show more </b>
</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.