【问题标题】:Gulp uglify causing AngularJS to not solve providerGulp uglify 导致 AngularJS 无法解决提供程序
【发布时间】:2015-12-29 19:15:34
【问题描述】:

虽然我熟悉使用 Angular 时的 uglify 问题,但我现在使用特定指令再次开始遇到此问题,即使我使用类数组样式进行依赖声明:

angular.module('app.directives').directive('domainImg', ['Endpoint', function (Endpoint) {
    return {
        restrict: 'A',
        controller: function ($scope, $element, $attrs) {
            $attrs.$set('ngSrc', Endpoint + $attrs.ngSrc);
        }
    };
}]);

我的主文件分别声明了模块。

angular.module('app.providers', []).constant('Endpoint', 'http://www.multzfidelidade.com.br/sistema/');
angular.module('app.tools', []);
angular.module('app.services', []);
angular.module('app.resources', []);
angular.module('app.controllers', []);
angular.module('app.directives', []);
angular.module('App', ['ui.mask', 'templates', 'app.providers', 'app.tools', 'app.services', 'app.resources', 'app.controllers', 'app.directives'])

现在,当我使用这个指令时,我遇到了 unknown eProvider 问题

错误:[$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=rProvider%20%3C-%20r

我是这样使用的:

<img class="prize-directive-image" ng-src="{{prize.image}}" domain-img/>

如果我删除 domain-img 标签,问题就会消失。另外,如果我不丑化代码,问题也会消失。

gulp.task('default', function () {
    // Concat all JS files into a single one called app.min.js
    gulp.src(['public_html/js/*.js', 'public_html/js/**/*.js', 'public_html/*.js', 'public_html/modules/**/*.js', '!public_html/app.min.js'])
        .pipe(concat('app.min.js'))
        // .pipe(uglify()) // Without this line the problem doesn't happen
        .pipe(gulp.dest('dist/'));

    // Concat all HTML directives into a single one
    gulp.src('public_html/js/**/**/*.html')
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(templateCache('templates.min.js', {standalone: true}))
        .pipe(gulp.dest('dist/'));
})

我希望我能对我在这个特定指令中可能出错的地方有所了解。

提前谢谢你。

【问题讨论】:

    标签: javascript angularjs dependency-injection gulp bundling-and-minification


    【解决方案1】:

    您错过了遵循指令控制器的 DI 的内联数组注释。

    代码

    controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
          $attrs.$set('ngSrc', Endpoint + $attrs.ngSrc);
    }]`
    

    【讨论】:

    • 所以基本上问题是我从未使用过内联控制器,在这种情况下,当我定义它时,它让我忘记了。非常感谢你,我永远不会抓住它。
    【解决方案2】:

    您需要像这样更改指令的controller 的语法:

    controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
            $attrs.$set('ngSrc', Endpoint + $attrs.ngSrc);
        }]
    

    查看this 了解更多详情。

    【讨论】:

      【解决方案3】:

      或者您可以保留它,并使用 GulpJS 为您做注释。在我的项目SkeletonSPA 中,我的做法完全相同。

      我使用 GulpJS 插件 gulp-ng-annotate 为我做这件事。您的 Gulp 任务如下所示:

      // require gulp-ng-annotate plugin
      let ngannotate = require('gulp-ng-annotate');
      
      // Concat all JS files into a single one called app.min.js
      gulp.src(['public_html/js/*.js', 'public_html/js/**/*.js', 'public_html/*.js', 'public_html/modules/**/*.js', '!public_html/app.min.js'])
        .pipe(ngannotate({gulpWarnings: false})) // annotate angular DI
        .pipe(concat('app.min.js'))
        .pipe(uglify()) // do the minification
        .pipe(gulp.dest('dist/'));
      

      这样你就不必自己做,你的 Gulp 任务会为你做 ;-)

      【讨论】:

      • 我见过这个功能,但我真的很喜欢我可以为自己指定 DI 的数组样式。这个问题的真正问题是我不知道问题是由DI引起的,因为我之前从未编写过内联控制器。虽然我不反对你的建议,但我只是更喜欢继续使用数组样式。
      猜你喜欢
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多