2

我有两个图像,一个绿色复选标记和一个灰色复选标记。当用户选中或取消选中列表中的项目时,这些会显示/隐藏。我遇到的问题是,当使用 缩小应用程序的图像时grunt-contrib-imagemin,这两个图像ng-src不会更改为缩小后的图像名称。任何想法为什么会发生这种情况以及如何解决它?

<img ng-if="item.checkedState === 'unchecked'" ng-src="images/unchecked.png" ng-click="changeCheckedState(item)">
<img ng-if="item.checkedState === 'checked'" ng-src="images/checked.png" ng-click="changeCheckedState(item)">

我的 usemin 任务:

usemin: {
  html: ['<%= yeoman.dist %>/{,*/}*.html'],
  css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
  options: {
    assetsDirs: ['<%= yeoman.dist %>','<%= yeoman.dist %>/images']
  }
}

编辑:通过测试@Tony Barnes 解决方案:

usemin: {
  html: ['<%= yeoman.dist %>/{,*/}*.html'],
  css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
  options: {
    assetsDirs: ['<%= yeoman.dist %>','<%= yeoman.dist %>/images'],
    patterns: {
    html: [
        [/<img[^\>]*[^\>\S]+ng-src=[""]([^'"\)#]+)(#.+)?["']/gm, 'Update the HTML with non standard ng-src attribute on img']
    ]
    }
  }
}

脚本和样式的其他 src 名称失败,即文件vendors132918.js进入vendor.js其中,然后由于未调用文件而src引发错误。是什么导致其他人在这里失败?据我所知,除了图像上的 src 之外,模式不应该改变任何东西..404 not foundvendor.jssrc

4

2 回答 2

4

问题grunt-usemin在于替换参考的任务。默认情况下,ng-src被 usemin 忽略。

如果您包含此html模式,则ng-src引用将被正确替换。

usemin: {
  options: {
    patterns: {
      html: [

        [/<img[^\>]*[^\>\S]+ng-src=[""]([^'"\)#]+)(#.+)?["']/gm, 'Update the HTML with non standard ng-src attribute on img']

      ]
    }
  }
}

(受此最近提交的启发)

但是,这会破坏对样式和脚本的其他引用。上面的添加似乎usemin覆盖了其他默认模式。

为了让这一切正常工作,您需要将上述模式和默认模式的 from 结合起来usemin

options: {
    assetsDirs: ['<%= yeoman.dist %>', '<%= yeoman.dist %>/images'],
    patterns: {
      html: [
         [/(images\/.*?\.(?:gif|jpeg|jpg|png|webp|svg))/gm,
         'Update the angular directives that ref revved images'],

         //defaults from node module
         [ /<script.+src=['"]([^"']+)["']/gm,
         'Update the HTML to reference our concat/min/revved script files'
         ],
         [ /<link[^\>]+href=['"]([^"']+)["']/gm,
         'Update the HTML with the new css filenames'
         ],
         [ /<img[^\>]+src=['"]([^"']+)["']/gm,
         'Update the HTML with the new img filenames'
         ],
         [ /data-main\s*=['"]([^"']+)['"]/gm,
         'Update the HTML with data-main tags',
         function (m) { return m.match(/\.js$/) ? m : m + '.js'; },
         function (m) { return m.replace('.js', ''); }
         ],
         [ /data-(?!main).[^=]+=['"]([^'"]+)['"]/gm,
         'Update the HTML with data-* tags'
         ],
         [ /url\(\s*['"]([^"']+)["']\s*\)/gm,
         'Update the HTML with background imgs, case there is some inline style'
         ],
         [ /<a[^\>]+href=['"]([^"']+)["']/gm,
         'Update the HTML with anchors images'
         ],
         [/<input[^\>]+src=['"]([^"']+)["']/gm,
         'Update the HTML with reference in input'
         ]
       ],
      js: [
          [/(images\/.*?\.(?:gif|jpeg|jpg|png|webp|svg))/gm, 
          'Update the JS to reference our revved images']
      ]
    }
  }
于 2015-03-16T12:17:56.590 回答
0

如果您在 ngSrc 指令值的开头使用一些大括号。你必须在你的 Gruntfile 中使用上面的正则表达式。

/<img[^\>]*[^\>\S]+ng\-src=['"][\{\}\:\w\s]*([^'"\)#]+)(#.+)?["']/gm

于 2015-03-17T14:38:26.880 回答