代码之家  ›  专栏  ›  技术社区  ›  Eric Mitjans

没有绝对字符串比较的AngularJS中的自定义orderBy?

  •  0
  • Eric Mitjans  · 技术社区  · 7 年前

    我正在为我的系统构建一个小型CSS美化器,它只会在CSS处于特定顺序时构建。 我设法做到了,但前提是粘贴在textarea中的代码没有属性。

    content
    margin
    font-size
    

    会有用的。

    但如果它是具有属性的正确代码:

    content: 'test';
    margin: 10px;
    font-size: 1em;
    

    那就不会了。

    我认为失败的是比较函数只会将字符串视为完整的,而不是部分的。

    a JS Bin .

    $scope.customOrder = function (item) {
      switch (item) {
        case 'display':
            return 1;
        case 'flex':
            return 2;
        case 'flex-basis':
            return 3;
        case 'flex-direction':
            return 4;
        ... 
      }
    };
    

    有什么办法解决这个问题吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   james00794    7 年前

    switch

    $scope.customOrder = function (item) {
       var parsedItem = item.substr(0, item.indexOf(':'));
       switch (parsedItem) {
         case 'display':
           return 1;
          case 'flex':
            return 2;
          ...