Angular 生产中缺少工厂 - Rails/Angular 应用程序

Angular factory missing in production - Rails/Angular App

我是开发训练营学校的一名学生,我创建了一个从独立 API 中提取数据的指标应用程序。为了接触前端框架,我在 Angular 中创建了仪表板。我创建了一个工厂,它通过检查主机名并将变量分配给 API URL.

来确定应用程序是否在开发或生产中 运行

我遇到的问题是工厂在开发中工作但不在生产中。事实上,当通过开发工具在生产环境中检查 application.js 时,工厂并不存在。以下是开发工具和生产工具中显示的代码。

发展:

angular.module('blocmetrics').factory('apiFactory', function(){
  var api = "";

  if (location.hostname == "localhost") {
    api = "http://localhost:3001";
    return api;
  } else {
    api = "https://ryanhaase-api-blocmetrics.herokuapp.com";
    return api;
  }
  return api;
});

angular.module('blocmetrics').controller('mainCtrl', function($scope, apiFactory, $http){

  $http.defaults.headers.common['Authorization'] = 'Token ' + document.cookie;

  $scope.goToDomain = function(domainId) {
    document.location = '#domains/' + domainId;
  };
  debugger;
  // API call for users apps
  var domain = $http.get(apiFactory +'/apps').
  success(function(data, status, headers, config) {
    $scope.domains = data;
  }).
  error(function(data, status, headers, config) {
    console.log('Error');
  });
});

产量(工厂缺失):

angular.module('blocmetrics').controller('mainCtrl', function($scope, $http){
  $http.defaults.headers.common['Authorization'] = 'Token ' + document.cookie;

  $scope.goToDomain = function(domainId) {
    document.location = '#domains/' + domainId;
  };
  // API call for users apps
  var domain = $http.get('http://localhost:3001/apps').
  success(function(data, status, headers, config) {
    $scope.domains = data;
  }).
  error(function(data, status, headers, config) {
    console.log('Error');
  });
});

如前所述,我是 Angular 和 JS 的新手,所以非常感谢您的帮助!

-R

Application.js:

var blocmetrics = angular.module('blocmetrics', ['ngResource', 'ngRoute', 'templates']);

blocmetrics.config(function($routeProvider, $locationProvider) {
  // $locationProvider.html5Mode(true);
  $routeProvider
  .when('/domains/:domain_id', {
    templateUrl: 'assets/templates/domain.html',
    controller: 'domainCtrl'
  })
  .when('/setup', {
    templateUrl: 'assets/templates/setup.html',
    controller: 'setupCtrl'
  })
  .otherwise({
    redirectTo: '/setup'
  });
});

angular.module('blocmetrics').factory('apiFactory', function(){
  var api = "";

  if (location.hostname == "localhost") {
    api = "http://localhost:3001";
    return api;
  } else {
    api = "https://ryanhaase-api-blocmetrics.herokuapp.com";
    return api;
  }
  return api;
});

angular.module('blocmetrics').controller('mainCtrl', function($scope, apiFactory, $http){

  $http.defaults.headers.common['Authorization'] = 'Token ' + document.cookie;

  $scope.goToDomain = function(domainId) {
    document.location = '#domains/' + domainId;
  };
  // API call for users apps
  var domain = $http.get(apiFactory +'/apps').
  success(function(data, status, headers, config) {
    $scope.domains = data;
  }).
  error(function(data, status, headers, config) {
    console.log('Error');
  });
});

angular.module('blocmetrics').controller('setupCtrl', function($scope, apiFactory, $http){

  $scope.cookie = document.cookie;

  $scope.domain = {};

  $scope.update = function(domain) {
    $http.post(apiFactory +'/apps', {
      'app': { domain }
    }).
    success(function(data, status, headers, config) {
      console.log('Success');
      $scope.reset();
    }).
    error(function(data, status, headers, config) {
      console.log('Error');
    });
  };
  $scope.reset = function() {
    $scope.domain = angular.copy($scope.master);
  };
  $scope.reset();
});

angular.module('blocmetrics').controller('domainCtrl', function($scope, $routeParams, apiFactory, $http) {
  // API call for an apps events
  var response = $http.get(apiFactory + '/apps/' + $routeParams.domain_id).
  success(function(data, status, headers, config) {
    $scope.events = data;
    $scope.app = data.data[Object.keys(data.data)[Object.keys(data.data).length - 1]];
    new Chartkick.ColumnChart("analytics", data.data.slice(0, -1));
  }).
  error(function(data, status, headers, config) {
    console.log('Error');
  });
});

如果您要缩小生产代码,您可能需要使用严格的依赖注入语法。

angular.module('blocmetrics').controller('mainCtrl', ['$scope', 'apiFactory', '$http',
  function($scope, apiFactory, $http) {

    $http.defaults.headers.common['Authorization'] = 'Token ' + document.cookie;

    $scope.goToDomain = function(domainId) {
      document.location = '#domains/' + domainId;
    };
    debugger;
    // API call for users apps
    var domain = $http.get(apiFactory + '/apps').
    success(function(data, status, headers, config) {
      $scope.domains = data;
    }).
    error(function(data, status, headers, config) {
      console.log('Error');
    });
  }
]);