【问题标题】:AngularJS and Rails view requires page refreshAngularJS 和 Rails 视图需要页面刷新
【发布时间】:2014-02-11 05:41:23
【问题描述】:

我在某些视图中有一个带有 AngularJS 的 Rails 应用程序。每当我单击将我带到页面的链接时,说/certifications 我看到了这个

然后当我重新加载页面或通过在 url 栏中按 enter 来请求它时,它会起作用并显示这个

这两个页​​面的重新加载方式是否存在差异,这会搞砸 AngularJS。我需要用户和角度路由器吗?我已将视图和控制器的代码放在下面。感谢您的帮助!

控制器:

@aquaticsApp = angular.module 'aquaticsApp',
  ['ngResource', 'aquaticsAppFilters']


@aquaticsApp.controller 'CertsCtrl', ['$scope', 'Certs',
  @CertsCtrl = ($scope, Certs) ->
    $scope.formatDate = (dateString)  ->
      d = new Date(dateString)

      curr_month = d.getMonth() + 1
      curr_date = d.getDate()
      curr_year = d.getFullYear()

      "#{curr_month}/#{curr_date}/#{curr_year}"

    $scope.sorter =
      value: 'firstName'

    Certs.get (data) ->
      $scope.certNames = data.certification_names
      $scope.users = data.users
]

查看:

<div ng-app='aquaticsApp' class='table-responsive'>
  <table ng-controller='CertsCtrl' class='table table-hover table-condensed'>
    <thead>
      <tr>
        <th ng-click='sorter.value="lastName"'>Last Name</th>
        <th ng-click='sorter.value="firstName"'>First Name</th>
        <th ng-click='sorter.value="location"'>Location</th>
        <th ng-repeat='certName in certNames' ng-click='sorter.value=certName.name'>
          {{certName.name}}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat='userData in users | orderBy:sorter.value'>
        <td>{{userData.lastName}}</td>
        <td>{{userData.firstName}}</td>
        <td>{{userData.location}}</td>
        <td ng-repeat='certName in certNames' class='{{userData[certName.name + "class"]}}'>
          {{formatDate(userData[certName.name])}}
        </td>
        </td>
      </tr>
    </tbody>
  </table>
</div>

【问题讨论】:

  • 在 Angular 编译和链接之前需要等待一段时间,然后表达式将被替换。代码对我来说似乎完全没问题。您可以尝试将 binds(double {) 放在一个 div 中,以避免在页面准备好之前显示它们。
  • 它似乎没有编译。表达式会无限期地停留在那里并且不会被替换,除非我刷新或通过地址栏访问页面。
  • 你确定包含了角脚本吗?
  • 巧妙的绑定技巧,使其在加载时不会显示{{}}。我试过了,它并没有解决我遇到的问题。你说得对!无论出于何种原因,当我单击此页面的链接时,该页面都没有加载我的任何资产。当我查看网络资源时,只显示 html 文件。
  • 唉,我和你一样困惑:)

标签: ruby-on-rails angularjs coffeescript


【解决方案1】:

在这里找到答案JQuery gets loaded only on page refresh in Rails 4 application

Turbolinks 会阻止绑定到准备好的文档的插件工作。由于我认为我会继续使用 turbolinks,因此我使用了上述 SO 答案中规定的解决方案并且它有效。

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多