相关疑难解决方法(0)

ngClass中的动态类名,角度为2

我需要在ngClass表达式中插入一个值,但我不能让它工作.

我试过这些解决方案,这是唯一对我有意义的解决方案,这两个解决方案都失败了:

<button [ngClass]="{'{{namespace}}-mybutton': type === 'mybutton'}"></button>
<button [ngClass]="{namespace + '-mybutton': type === 'mybutton'}"></button>
Run Code Online (Sandbox Code Playgroud)

这个与插值一起使用但是由于整个字符串被添加为类而失败了动态添加的类:

<button ngClass="{'{{namespace}}-mybutton': type === 'mybutton'}"></button>
Run Code Online (Sandbox Code Playgroud)

所以我的问题是你如何使用这样的动态类名ngClass

angular

99
推荐指数
5
解决办法
10万
查看次数

如何使用ngClass的动态值

我正在尝试应用与范围变量相同的类名.

例如:

<div ng-class="{item.name : item.name}">

这样就可以将值item.name添加到类中.但这似乎没有做任何事情.有关如何做到这一点的任何建议?

谢谢!

编辑:

这实际上是使用ng-options在select中完成的.例如:

<select ng-options="c.code as c.name for c in countries"></select>

现在,我想应用一个值为的类名 c.code

我找到了以下指令,它似乎有效,但没有插值:

angular.module('directives.app').directive('optionsClass', ['$parse', function ($parse) {
  'use strict';

  return {
    require: 'select',
    link: function(scope, elem, attrs, ngSelect) {
      // get the source for the items array that populates the select.
      var optionsSourceStr = attrs.ngOptions.split(' ').pop(),
      // use $parse to get a function from the options-class attribute
      // that you can use to evaluate later.
          getOptionsClass = $parse(attrs.optionsClass);

      scope.$watch(optionsSourceStr, …
Run Code Online (Sandbox Code Playgroud)

angularjs

31
推荐指数
4
解决办法
6万
查看次数

标签 统计

angular ×1

angularjs ×1