0

我在我在 rails 4 上构建的应用程序中使用 AngularJS。我遇到的问题是,当我第一次重定向到页面时,它不会加载任何 js。如果我输入特定的 URL 或刷新页面,那么它工作正常。在 chrome 控制台中,我收到错误消息:

Uncaught Error: No module:  angular.js?body=1:1212

这是我的 application.js 文件:

    // This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require angular
//= require_tree .


var Sampleblog = angular.module('Sampleblog', ['ngResource'])

$(document).on('page:load', function() {
  return $('[ng-app]').each(function() {
    var module;
    module = $(this).attr('ng-app');
    return angular.bootstrap(this, [module]);
  });
});

相关视图文件:

<h1>Related Tweets</h1>

<div ng-app>
    <div ng-controller="TermCtrl">
      <span>{{remaining()}} of {{terms.length}} selected</span>
      <ul class="unstyled">
        <li ng-repeat="term in terms">
          <input type="checkbox" ng-model="term.select">
          <span class="select-{{term.select}}">{{term.text}}</span>
        </li>
      </ul>
      <form ng-submit="addTerm()">
        <input type="text" ng-model="termText"  size="30"
               placeholder="add new term here">
        <input class="btn-primary" type="submit" value="add">
      </form>
    </div>
</div>

<%= link_to 'Back to Posts', posts_path, type: "button", class: "btn btn-primary" %>

以及相关的 .js 文件

function TermCtrl($scope) {

    $scope.terms = [
    {text:'Placeholder1', select:true},
    {text:'Placeholder2', select:false}];

  $scope.addTerm = function() {
    $scope.terms.push({text:$scope.termText, select:false});
    $scope.termText = '';
  };

  $scope.remaining = function() {
    var count = 0;
    angular.forEach($scope.terms, function(term) {
      count += term.select ? 1 : 0;
    });
    return count;
  };
}
TermCtrl.$inject = ['$scope'];

任何帮助,将不胜感激!

4

1 回答 1

0

您只需要告诉 Angular 您要加载哪个模块ng-app

<div ng-app="Sampleblog">

否则,它不知道您要加载这个:

angular.module('Sampleblog', ['ngResource'])
于 2013-11-07T16:10:27.177 回答