簡體   English   中英

Angular自定義指令在過濾器中使用范圍變量

[英]Angular custom directive use scope variable in filter

我的角度自定義指令遇到了一些麻煩。 我想訪問我的過濾器中的“類型”變量,它是filterung對象(在第一層,因此無需自定義過濾器就應該可以)。

這是(到目前為止非常基本的)結構:

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>'
  }
});

有沒有辦法訪問模板字符串中的變量? 轉義或使用模板html文件都不適合我。

謝謝克里斯

使用鏈接函數從范圍變量構造過濾器。

它稍顯尷尬,並且在將來更改過濾器時將為您提供更大的靈活性。

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 }
    }
  }
});

這是一個工作的小插曲 ,您可以將整個過濾器對象傳遞給指令,而不僅僅是一個字符串-這樣,您可以在不更改指令的情況下對任何想要的內容進行過濾

重命名變量並重寫指令后,它突然起作用:

    <li ng-repeat="information in list | filter:{ttype:type}">{{information.value}}</li>

有時它只是..尷尬。 不過:謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM