简体   繁体   English

Angular Noob + Yeoman:“ app”未定义

[英]Angular noob + Yeoman: 'app' is not defined

I built a to do list you can see here: https://github.com/EdmundMai/angular_todolist 我建立了一个待办事项清单,您可以在这里查看: https : //github.com/EdmundMai/angular_todolist

In my controller, I can access the toDoListApp globally anywhere I want. 在我的控制器中,我可以在任何需要的地方全局访问toDoListApp

However, now I'm starting a new project with Yeoman and I have the same setup: 但是,现在我与Yeoman开始一个新项目,并且具有相同的设置:

app.js: app.js:

'use strict';

var app = angular.module('angNewsApp', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute'
  ]);


app.config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/posts.html',
        controller: 'PostsCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

posts.js: posts.js:

'use strict';

app.controller('PostsCtrl', function($scope) {
});

posts.html: posts.html:

<p>{{hey}}zzzzz</p>

However, jshint is complaining and my view isn't showing. 但是,jshint抱怨并且我的观点没有显示。

app/scripts/controllers/posts.js
  line 3  col 1   'app' is not defined.
  line 3  col 44  '$scope' is defined but never used.

Why is it that I can't use app? 为什么我不能使用应用程序?

UPDATE UPDATE

the include tags of my index.html: 我的index.html的包含标签:

<!-- build:js scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<!-- endbower -->
<!-- endbuild -->

    <!-- build:js({.tmp,app}) scripts/scripts.js -->
    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/posts.js"></script>
    <!-- endbuild -->

What should I do with these? 我该怎么办?

It's because you have separated your JavaScript in two file. 这是因为您已将JavaScript分为两个文件。 You need to bring it to one file. 您需要将其保存到一个文件中。

  1. You can compile with the --out option to get a single file. 您可以使用--out选项进行编译以获取单个文件。
  2. Include all files into script tags (Order is important). 将所有文件包括到脚本标签中(顺序很重要)。
  3. Use a module loader like RequireJS. 使用诸如RequireJS之类的模块加载器。

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

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