Javascript AngularJS未在第一页视图上加载
我在rails 4上构建的应用程序中使用AngularJS。我遇到的问题是,当我第一次重定向到一个页面时,它没有加载任何js。如果我输入特定的URL或刷新页面,那么它工作正常。在chrome控制台中,我得到一个错误:Javascript AngularJS未在第一页视图上加载,javascript,ruby-on-rails,angularjs,Javascript,Ruby On Rails,Angularjs,我在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
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" %>
任何帮助都将不胜感激 您只需告诉angular您要在
ng应用程序中加载哪个模块:
<div ng-app="Sampleblog">
无论何时我添加它,它都不会加载,甚至在刷新时也不会加载。您是否在index.html上的
中引用angular.js
?我的posts.js文件中有它。因此,它将在posts/index页面上引用它,我想你必须给出一个更完整的代码表示。最好是用plunker或JSFIDLE演示这个问题。
<div ng-app="Sampleblog">
angular.module('Sampleblog', ['ngResource'])