Angular custom directive use scope variable in filter

Chris

I am having a little trouble with my angular custom directive. I want to access the "type"-variable in my filter, which is filterung objects (on the first level, so it should be possible without a custom filter).

This is the (very basic so far) structure:

angular.module('....').directive('ngTest', function () {
return {
  restrict: 'AE',
  replace: 'true',
  scope: {
    list: '=',
    type: '@'
  },
  template: '<div><ul><li ng-repeat="information in list | filter:{ttype:type}">....</li></ul></div>'
  }
});

Is there a way to access the variable in the template-string? Neither escaping nor using a template html file worked for me..

Thanks, Chris

Dave

Construct your filter from the scope variables using the link function.

It's slightly less awkward, and it will give you more flexibility to alter the filter in the future.

angular.module('....').directive('ngTest', function () {
  return {
    restrict: 'AE',
    replace: 'true',
    scope: {
      list: '=',
      type: '@'
    },
    template: '<div><ul><li ng-repeat="information in list | filter:filterOb">....</li></ul></div>',
    link: function(scope, element, attrs) {
      scope.filterOb = { ttype: scope.type }
    }
  }
});

Here is a working plunk where instead you pass the whole filter object to the directive, instead of just a string - that way you can filter on anything you want without altering the directive

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access to custom filter from an Angular directive with isolate scope

From Dev

Angular JS custom directive - Attribute binding to string or scope variable

From Dev

Angular + Typescript: Use Directive Class Scope Variable in Controller Class

From Dev

Angular custom directive scope not updating as expected

From Dev

Angular custom directive - $scope.$watch not firing

From Dev

how to use isolated scope in custom directive?

From Dev

Angular directive: bind to variable in parent scope

From Dev

Angular moving scope variable from controller to directive

From Dev

How to use Angular scope with a service request and a directive

From Dev

Use of isolate scope - @, &, and = in a simple Angular Directive

From Dev

how to use scope in angular directive 4

From Dev

Why Angular custom directive scope effects parent controller scope?

From Dev

Why Angular custom directive scope effects parent controller scope?

From Dev

Angular parent scope not updated when directive updates the same scope variable

From Dev

AngularJs - Use custom filter inside directive controller

From Dev

AngularJS Passing scope to directive via a custom filter function $rootScope:infdig

From Dev

How to use angular translate directive with limitTo filter

From Dev

AngularJS - Setting a controller's $scope variable in a custom directive that accesses the DOM

From Dev

angularJS custom directive unable to show scope string variable

From Dev

Can I filter by a property and scope variable in angular?

From Dev

how to make custom directive or pass variable in directive in angular js

From Dev

Angular scope from ng-controller on custom directive that uses transclusion

From Dev

How to change ng-model scope value in custom Angular directive?

From Dev

Angular custom directive with isolate scope not firing ng-click function

From Dev

How to decide which angular custom directive to use

From Dev

Accessing scope variable in directive

From Java

Use of symbols '@', '&', '=' and '>' in custom directive's scope binding: AngularJS

From Dev

Angular scope's variable from directive not syncing in ng-style

From Dev

Accessing scope variable from controller in a directive in angular.js

Related Related

  1. 1

    Access to custom filter from an Angular directive with isolate scope

  2. 2

    Angular JS custom directive - Attribute binding to string or scope variable

  3. 3

    Angular + Typescript: Use Directive Class Scope Variable in Controller Class

  4. 4

    Angular custom directive scope not updating as expected

  5. 5

    Angular custom directive - $scope.$watch not firing

  6. 6

    how to use isolated scope in custom directive?

  7. 7

    Angular directive: bind to variable in parent scope

  8. 8

    Angular moving scope variable from controller to directive

  9. 9

    How to use Angular scope with a service request and a directive

  10. 10

    Use of isolate scope - @, &, and = in a simple Angular Directive

  11. 11

    how to use scope in angular directive 4

  12. 12

    Why Angular custom directive scope effects parent controller scope?

  13. 13

    Why Angular custom directive scope effects parent controller scope?

  14. 14

    Angular parent scope not updated when directive updates the same scope variable

  15. 15

    AngularJs - Use custom filter inside directive controller

  16. 16

    AngularJS Passing scope to directive via a custom filter function $rootScope:infdig

  17. 17

    How to use angular translate directive with limitTo filter

  18. 18

    AngularJS - Setting a controller's $scope variable in a custom directive that accesses the DOM

  19. 19

    angularJS custom directive unable to show scope string variable

  20. 20

    Can I filter by a property and scope variable in angular?

  21. 21

    how to make custom directive or pass variable in directive in angular js

  22. 22

    Angular scope from ng-controller on custom directive that uses transclusion

  23. 23

    How to change ng-model scope value in custom Angular directive?

  24. 24

    Angular custom directive with isolate scope not firing ng-click function

  25. 25

    How to decide which angular custom directive to use

  26. 26

    Accessing scope variable in directive

  27. 27

    Use of symbols '@', '&', '=' and '>' in custom directive's scope binding: AngularJS

  28. 28

    Angular scope's variable from directive not syncing in ng-style

  29. 29

    Accessing scope variable from controller in a directive in angular.js

HotTag

Archive