简体   繁体   English

AngularJS未在首页视图上加载

[英]AngularJS not loading on first page view

I'm using AngularJS in an app that I built on rails 4. The problem I'm running into is that when I first redirect to a page, it doesn't load any of the js. 我在基于Rails 4构建的应用程序中使用AngularJS。我遇到的问题是,当我第一次重定向到页面时,它不会加载任何js。 If I type in the specfic URL or refresh the page then it works fine. 如果我输入特定的URL或刷新页面,则可以正常工作。 In chrome console, I'm getting the error: 在Chrome控制台中,出现错误:

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

Here's my application.js file: 这是我的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]);
  });
});

The relevant view file: 相关视图文件:

<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" %>

And the relevant .js file 以及相关的.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'];

Any help would be appreciated! 任何帮助,将不胜感激!

You just need to tell angular which module you want to load in ng-app : 您只需要告诉angular您要在ng-app加载哪个模块:

<div ng-app="Sampleblog">

Otherwise, it doesn't know that you want to load this one: 否则,它不知道您要加载此代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM