在JS文件缩小后,$ http没有注入组件中的控制器

时间:2016-10-14 21:50:07

标签: angularjs minify angularjs-components

angular.module("datasView",[])
.component("datasView",{
    templateUrl : 'dataview/datasview.template.html',
    controller : function control($http){
        var self = this;
        $http.get('src/data/issues.json').then(function(response){
            // console.log(response.data[0].closed_timestamp);
            self.issueinfo = response.data;
            for(var i = 0; i < response.data.length; i++){
                if(self.issueinfo[i].closed_timestamp == ""){
                    self.issueinfo[i].closed_timestamp = "Open";
                }
            }
        });
    }
});

这是我的代码。当我缩小这个js文件时,参数中的$ http会转换为其他一些随机变量,因此不会从本地json文件中获取数据。我如何纠正这个问题,因为它是一个项目,缩小是必须的。

2 个答案:

答案 0 :(得分:1)

在控制器定义中添加[]以及'$http'

controller : [ '$http',function control($http){ 

}]

完整示例

angular.module("datasView",[])
.component("datasView",{
    templateUrl : 'dataview/datasview.template.html',
    controller : [ '$http',function control($http){ //opening => [
        var self = this;
        $http.get('src/data/issues.json').then(function(response){
            // console.log(response.data[0].closed_timestamp);
            self.issueinfo = response.data;
            for(var i = 0; i < response.data.length; i++){
                if(self.issueinfo[i].closed_timestamp == ""){
                    self.issueinfo[i].closed_timestamp = "Open";
                }
            }
        });
    }]//closing => ]
});

您可以在此处阅读Declaring AngularJS Modules For Minification

答案 1 :(得分:0)

依赖注入的另一种方式是 $ injector property annotation https://docs.angularjs.org/guide/di#-inject-property-annotation):

(function (angular) {

  'use strict';

  angular.module("datasView",[]).component('datasView', {
    templateUrl : 'dataview/datasview.template.html',
    controller : controllerFunction
  });

  controllerFunction.$inject = ['$http'];
  function controllerFunction ($http) {
    var self = this;
    $http.get('src/data/issues.json').then(function(response){
        // console.log(response.data[0].closed_timestamp);
        self.issueinfo = response.data;
        for(var i = 0; i < response.data.length; i++){
            if(self.issueinfo[i].closed_timestamp == ""){
                self.issueinfo[i].closed_timestamp = "Open";
            }
        }
    });
  }

});

})(角度);