【问题标题】:Getting [ng:areq] Argument 'profileCtrl' is not a function, got undefined in production. Controller works just fine locally获取 [ng:areq] 参数 'profileCtrl' 不是函数,在生产中未定义。控制器在本地工作得很好
【发布时间】:2017-03-02 20:22:53
【问题描述】:

这是我的 profileCtrl

(function() {
  "use strict";

  angular.module("app").controller("profileCtrl",['$scope', '$http', function($scope,$http) {
    $scope.getProfiles = function() {
      $http.get("/api/v1/profiles.json").then(function(response) {
        $scope.approvedDonors = response.data.approved;
        $scope.unapprovedDonors = response.data.unapproved;
        $scope.donorCount = $scope.unapprovedDonors.length;
      });
    };

    $scope.toggleApprove = function(donor) {
        donor.approved = donor.approved ? 0 : 1;
        var donorParams = {
          id: donor.id,
          approved: donor.approved
        };
        $http.patch('/api/v1/profiles/' + donor.id + '.json', donorParams).success(function(response) {
          var index = $scope.unapprovedDonors.indexOf(donor);
          $scope.unapprovedDonors.splice(index, 1);
          $scope.approvedDonors.push(donor);
        });
        $scope.donorCount -= 1;
    };

    $scope.adminFoodPickups = function() {
      $http.get('/api/v1/food_pickups.json').then(function(response) {
        $scope.approvedPickups = response.data.approved;
        $scope.unapprovedPickups = response.data.unapproved;
      });
    };
  window.$scope = $scope;
  }]);

})();

这是我需要 profileCtrl:

的地方
<div ng-controller='profileCtrl' ng-init='getProfiles()'>
    <div class="row fullPageHeight">
        <div class="col-lg-12">
            <h1 class="page-header">Donor Profiles</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Donor Profiles Awaiting Approval
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <table width="100%" class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>Profile Submitted On</th>
                                <th>Business Name</th>
                                <th>Donor Type</th>
                                <th>Contact Name</th>
                                <th>Phone</th>
                                <th>Email</th>
                                <th>Address</th>
                                <th>Status</th>
                            </tr>
                        </thead>


                        <tbody>
                            <tr class="odd gradeX" ng-repeat='donor in unapprovedDonors'>
                              <td>{{donor.created_at | date:'fullDate'}}</td>
                              <td><a href="/profiles/{{donor.id}}">{{donor.business_name}}</a></td>
                              <td>{{donor.type_of_donor}}</td>
                              <td>{{donor.contact_name}}</td>
                              <td>{{donor.phone_number}}</td>
                              <td>{{donor.email}}</td>
                              <td>{{donor.address}}</td>
                              <td class='btn btn-warning' ng-click="toggleApprove(donor)"> {{"Pending - Click to Approve"}}</td>
                            </tr>


                        </tbody>
                    </table>
                  </div>
                  <div class="panel-heading">
                      Approved Donor Profiles
                  </div>
                    <div class="panel-body">
                        <table width="100%" class="table table-striped table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>Profile Submitted On</th>
                                    <th>Business Name</th>
                                    <th>Donor Type</th>
                                    <th>Contact Name</th>
                                    <th>Phone</th>
                                    <th>Email</th>
                                    <th>Address</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                        <tbody>
                            <tr class="odd gradeX" ng-repeat='donor in approvedDonors'>
                              <td>{{donor.created_at | date:'fullDate'}}</td>
                              <td><a href="/profiles/{{donor.id}}">{{donor.business_name}}</a></td>
                              <td>{{donor.type_of_donor}}</td>
                              <td>{{donor.contact_name}}</td>
                              <td>{{donor.phone_number}}</td>
                              <td>{{donor.email}}</td>
                              <td>{{donor.address}}</td>
                              <td class='success'> {{"Approved"}}</td>
                            </tr>
                        </tbody>
                      </table>
                    </div>
              </div>
          </div>
          <div align="center"><%= link_to "Return to Home Page", admin_path, class: "btn btn-primary" %></div>
      </div>
</div>

这是我的 app.js:

(function() {
  "use strict";

  angular.module("app", []);

}());

我在所有这些代码之前初始化我的 ng-app='app' 因为我在 application.html 的 rails 布局文件夹中有它。有没有人知道为什么只有在部署到heroku之后才会发生这种情况?

【问题讨论】:

  • 在您的索引页面中,您是否正确包含了包含 profileCtrl 的脚本
  • @Ladmerc 我试过了,但无济于事,它看起来像这样:跨度>

标签: javascript ruby-on-rails angularjs heroku


【解决方案1】:

您是否在生产部署期间缩小/连接/移动文件?

例如,如果您使用 grunt/gulp 来缩小和连接产品文件,那么您需要确保您的索引页面包含正确的脚本引用。

【讨论】:

  • 当我打开 chrome 检查时,它说这个脚本正在被加载/引用:
  • 使用生产中的参考,您可以访问您的控制器文件吗?
猜你喜欢
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
相关资源
最近更新 更多