简体   繁体   English

角度形式的自定义类型控制器在部署时失败

[英]Angular-Formly Custom Type Controller Fails on Deployment

I'm using the example for a repeating section given on the angular-formly website. 我将这个示例用于角度形式的网站上给出的重复部分。 In my app.config I am creating a new type like this: 在我的app.config中,我正在创建一个像这样的新类型:

var unique = 1;

formlyConfigProvider.setType({
  name: 'repeatSection',
  templateUrl: 'app/rawMaterial/repeatSection.html',
  controller: function($scope) {
    $scope.formOptions = {formState: $scope.formState};
    $scope.addNew = addNew;

    $scope.copyFields = copyFields;


    function copyFields(fields) {
      fields = angular.copy(fields);
      addRandomIds(fields);
      return fields;
    }

    function addNew() {
      $scope.model[$scope.options.key] = $scope.model[$scope.options.key] || [];
      var repeatsection = $scope.model[$scope.options.key];
      var lastSection = repeatsection[repeatsection.length - 1];
      var newsection = {};
      if (lastSection) {
        newsection = angular.copy(lastSection);
      }
      repeatsection.push(newsection);
    }

    function addRandomIds(fields) {
      unique++;
      angular.forEach(fields, function(field, index) {
        if (field.fieldGroup) {
          addRandomIds(field.fieldGroup);
          return; // fieldGroups don't need an ID
        }

        if (field.templateOptions && field.templateOptions.fields) {
          addRandomIds(field.templateOptions.fields);
        }

        field.id = field.id || (field.key + '_' + index + '_' + unique + getRandomInt(0, 9999));
      });
    }

    function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min)) + min;
    }
  }

  });

Everything works fine on my local machine, but when I deploy to Heroku I get the following error: 在本地计算机上一切正常,但是当我部署到Heroku时,出现以下错误:

Error: [$injector:unpr] Unknown provider: aProvider <- a

If I remove the controller: section from the type definition the error goes away. 如果我从类型定义中删除controller:部分,错误将消失。 Leaving the controller: definition in but commenting out all of its contents still throws the error. 离开controller:定义但将其所有内容注释掉仍会引发错误。

I'm using the angular-fullstack yeoman generator with the default Grunt tasks. 我正在使用带有默认Grunt任务的angular-fullstack yeoman生成器。

If you're throwing an error based on an expected service called a , this is an effect of minification in the AngularJS ecosystem. 如果你是基于预期的服务叫做抛出错误a ,这是微小的在AngularJS生态系统的影响。 I'm suspecting $scope is being transformed to a when minified. 我怀疑$scope会被缩小为a To preserve $scope , define your controller with a "min-safe" injection signature. 要保留$scope ,请使用“最小安全”注入签名定义控制器。 There are various ways to do this, but here is a common pattern. 有多种方法可以执行此操作,但这是一种常见模式。 Observe the following... 注意以下几点...

controller: ['$scope', function($scope) {
    /* ... */
}])

JSFiddle Link - simple demo reproducing error JSFiddle Link-简单的演示再现错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM