简体   繁体   English

某个元素上的触发函数-ngrepeat-angularjs

[英]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: 一些建议:

  • Use ng-init with care. 小心使用ng-init。 I know it will tempt us but always try to put logic inside controllers and services; 我知道它将吸引我们,但始终尝试将逻辑放入控制器和服务中。
  • Avoid assignments inside templates; 避免在模板内进行分配;
  • Learn how to use controllerAs syntax. 了解如何使用controllerAs语法。 It gives you an object to write your models to ( the controller ), so solves most problems related to scope inheritance; 它为您提供了一个将模型写入( 控制器 )的对象,从而解决了大多数与范围继承相关的问题;
  • Do not inject $scope, put your view models inside controllers. 不要注入$ scope,将视图模型放入控制器中。

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.

相关问题 AngularJS ngRepeat元素删除 - AngularJS ngRepeat element removal 如何在函数返回的元素上使用ngRepeat(实际上是一个过滤器) - AngularJS? - How to use ngRepeat on element returned from a function (a filter, actually) - AngularJS? AngularJS在同一元素上嵌套了ngRepeat - AngularJS nested ngRepeat on same element angularjs ngRepeat指令不会触发列表中其他项的摘要 - angularjs ngRepeat directive to not trigger digest for other items in the list AngularJS 中 ngRepeat 中的 ngRepeat 问题 - Problem with ngRepeat inside ngRepeat in AngularJS AngularJS ngRepeat-听元素运动(我需要使用ngRepeat渲染iframe列表) - AngularJS ngRepeat - listening to element movement (I need to render a list of iframes with ngRepeat) AngularJS-ngRepeat在类字典对象中删除的元素上未更新 - AngularJS - ngRepeat not updated at element deleted in dictionary-like object 是否可以在AngularJS中为ng-repeat的每个元素触发一个函数? - Is it possible to trigger a function for each element of an ng-repeat in AngularJS? AngularJS:检测ngRepeat中的哪个复选框已选中/未选中并调用函数 - AngularJS: Detecting which checkbox in an ngRepeat is checked/unchecked and calling a function 销毁ngRepeat中的作用域时,$ destroy函数中的AngularJS错误引用 - AngularJS wrong reference in $destroy function while destruction of the scope in ngRepeat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM