我在我在rails 4上构建的应用程序中使用AngularJS。我遇到的问题是,当我第一次重定向到页面时,它不会加载任何js。如果我输入specfic 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'];
任何帮助将不胜感激!
答案 0 :(得分:0)
您只需要在ng-app
:
<div ng-app="Sampleblog">
否则,它不知道你要加载这个:
angular.module('Sampleblog', ['ngResource'])