What is the best way to conditionally apply a class?

后端 未结 22 2236
感动是毒
感动是毒 2020-11-22 09:12

Lets say you have an array that is rendered in a ul with an li for each element and a property on the controller called selectedIndex.

22条回答
  •  渐次进展
    2020-11-22 09:39

    If you want to go beyond binary evaluation and keep your CSS out of your controller you can implement a simple filter that evaluates the input against a map object:

    angular.module('myApp.filters, [])
      .filter('switch', function () { 
          return function (input, map) {
              return map[input] || '';
          }; 
      });
    

    This allows you to write your markup like this:

    ...

提交回复
热议问题