簡體   English   中英

將變量傳遞給自定義Angular指令

[英]Pass variable to custom Angular directive

我有一個手風琴清單,我希望能夠在其中打開某些物品。 我創建了手風琴打開指令,如果shouldBeOpen為true,則該指令應打開特定項目

這是HTML元素(項目以循環方式打印)

<div class="m-list-item-head accordion-item" accordion-open="{{shouldBeOpen}}">

這是我的自定義指令

(function () {
  'use strict';

  angular.module('myApp').directive('accordionOpen', function () {
    return {
      restrict: 'A',
      link: function(scope, element) {
          if (open) {
            $(element).closest('.accordion-item').siblings().slideDown('fast');
            $(element).closest('.accordion-item').addClass('opened');
          }
      }
    }
  });
})();

我無法閱讀指令中的手風琴打開的內容。

不需要插值表達式。 因此更改為:

accordion-open="shouldBeOpen"

接下來,將attrs添加到鏈接函數中:

link: function(scope, element, attrs)

並使用$eval獲取鏈接函數中屬性的值:

console.log(scope.$eval(attrs.accordionOpen))

暫無
暫無

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

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