我需要在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?
我正在尝试应用与范围变量相同的类名.
例如:
<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)