문제

I have the following User class:

class User {
  String name;
  bool registered;
  User(this.name, [this.registered=false]);
}

In my controller, I create users, a list of User objects, some of which have the registered value set to true. How can I filter based on the registered property? Here is my incomplete html:

<div ng-repeat="user in ctrl.users | filter: ????">
  {{user.name}}
</div>

Because of https://github.com/angular/angular.dart/issues/800, I cannot use a predicate function. Is there a way to get a handle on the user variable in the filter and do a boolean filter? Something like this:

// NOT REAL CODE.
<div ng-repeat="user in ctrl.users | filter:user.registered">
  {{user.name}}
</div>
도움이 되었습니까?

해결책

In this particular case, you want to use the "FilterFilter", which allows you to filer on properties of objects in a list.

<li ng-repeat="user in ctrl.users | filter:{registered: true}">{{user.name}}</li>

Full documentation is at https://docs.angulardart.org/#angular/angular-filter.FilterFilter

Second, if you need a more complicated predicate, the work-around to issue 800 is to make your predicate functions member variables instead of methods on your controller.

e.g.

@NgController()
class Controller {
  Function pred = (x) => shouldXByDisplayed(x);
}

(but expect the bug to be fixed soon!)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top