【问题标题】:Angular factory missing in production - Rails/Angular App生产中缺少 Angular 工厂 - Rails/Angular App
【发布时间】:2015-07-27 14:54:30
【问题描述】:

我是开发训练营学校的一名学生,我创建了一个从独立 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');
  });
});

【问题讨论】:

  • 查看实际的application.js 文件可能会有所帮助。您是将控制器和工厂的代码放在application.js 还是单独的文件中?
  • 如果它丢失与角度无关......听起来像是构建过程问题
  • 你缩小代码了吗?
  • 控制器和工厂都在 application.js 中(在上面添加)。我不确定代码是否被缩小,但我确实设置了:config.assets.js_compressor = Uglifier.new(mangle: false)

标签: javascript ruby-on-rails angularjs environment-variables factory


【解决方案1】:

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

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');
    });
  }
]);

【讨论】:

  • 嘿,justind,这在开发中有效,但在生产中,什么都没有。工厂仍然丢失,并且依赖项不存在。这几乎就像 Heroku 上的生产代码库没有更新一样。
  • 我运行了rake assets:precompile,它更新了我在生产中的资产。关于工厂,看起来一切正常。现在我只是在 heroku 找到我的角度模板时遇到问题。控制台错误为:GET https://ryanhaase-blocmetrics.herokuapp.com/assets/templates/setup.html 404 (Not Found)
猜你喜欢
  • 2014-07-06
  • 2015-07-12
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 2015-03-19
  • 2014-02-07
  • 1970-01-01
相关资源
最近更新 更多