【问题标题】:Pass a variable from the DOM to SCSS将变量从 DOM 传递到 SCSS
【发布时间】:2020-03-31 17:09:04
【问题描述】:

我在 .scss 中使用 angular,我想知道如何在 .scss 中执行以下 .less 代码。

HTML

<span class="flag-icon-{{territoryId}}"></span>

CSS

.flag-icon-@{country} {
  //@country being the {{territoryId}} from above
}

这可能吗?

【问题讨论】:

    标签: angular sass


    【解决方案1】:

    HTML

    <span class="flag-icon-{{territoryId}}"></span>
    

    scss 文件

    $country-collection: ('india', 'usa', 'germany', 'pakistan');
    
    @for $country from 0 to length($country-collection) {
        .flag-icon-#{$country} {
            color: red;
        }
    }
    

    【讨论】:

    • 感谢您的回答,它不太奏效!但我设法弄清楚了,它与下面的代码一起工作!非常感谢!
    【解决方案2】:

    谢谢Sayooj VRhttps://stackoverflow.com/users/10647431/sayooj-v-r

    我尝试了你的上述方法,但它似乎没有用,但它让我更了解了我可以用 scss 做什么,我设法用下面的代码弄明白了(哇哦!):

    HTML

    <span class="flag-icon-{{territoryId}}"></span>
    

    SCSS

    $country-collection: ('india', 'usa', 'germany', 'pakistan');
    
    @each $country in $country-collection {
        .flag-icon-#{$country} {
          color: red
        }
    }
    

    @for 似乎由于某种原因(不知道为什么)没有工作,所以我尝试了一种不同的循环技术,使用 @each 方法,并且 walaaaah 它工作了!

    @each 的文档在这里:https://sass-lang.com/documentation/at-rules/control/each

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 2019-03-17
      • 2017-09-06
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2017-06-03
      相关资源
      最近更新 更多