简体   繁体   English

Angular中的app.js到底是什么?

[英]What exactly is app.js in Angular?

I'm learning Angular for a new job (1 at the moment, then we'll be transitioning to 2 later on). 我正在学习Angular的一份新工作(目前为1个,然后我们将过渡到2个)。 I have experience with ASP MVC but none with client side MVC so this is all new to me from the get go. 我有使用ASP MVC的经验,但没有使用客户端MVC的经验,因此从一开始我对这一切都是新的。 Most tutorials use this file "app.js" or something along these lines, which I guess registers various things with Angular, like the name of your web app, etc. That's all fine, but I want to know what is the specific purpose of this file? 大多数教程都使用此文件“ app.js”或类似的东西,我想这些东西会在Angular中注册各种东西,例如您的Web应用程序的名称等。这很好,但是我想知道它的具体目的是什么。这个文件?

For example, I'm going through this tutorial currently and this is the code we're doing for routing and controllers: 例如,我目前正在阅读本教程,这是我们为路由和控制器执行的代码:

var AngularWebApp = angular.module('AngularWebApp', []);

AngularWebApp.controller
    ('LandingPageController', LandingPageController);

var configFunction = function ($routeProvider) {
    $routeProvider.
        when('/routeOne', {
            templateUrl: 'routesDemo/one'
        })
        .when('/routeTwo', {
            templateUrl: 'routesDemo/two'
        })
        .when('/routeThree', {
            templateUrl: 'routesDemo/three'
        });
}
configFunction.$inject = ['$routeProvider'];

The reason I'm asking is I have yet to read a single explanation anywhere on what this file is actually for, it seems like we're just dumping a bunch of config settings in here, so is this sort of like in ASP MVC the web.config file? 我要问的原因是,我还没有在任何地方阅读关于此文件实际用途的任何解释,似乎我们只是在此处转储了一堆配置设置,所以在ASP MVC中就像web.config文件? I just want a definitive answer rather than just guessing about it. 我只是想要一个明确的答案,而不仅仅是猜测。

app.js (I use main.js ) is just a common name given to the root bootstrapping file for an angular project. app.js (我使用main.js )只是为角度项目的根引导文件指定的通用名称。 While you may concat all your js files for a production release, when developing, file separation is obviously a good thing. 尽管可以将所有js文件合并为生产版本,但是在开发时,文件分离显然是一件好事。

When developing an angular project, you create a bunch of modules that do various things, but you need a primary module for your app that you use with the ng-app attribute in your html: https://docs.angularjs.org/api/ng/directive/ngApp 在开发有角度的项目时,您会创建一堆执行各种功能的模块,但是您需要为应用程序使用一个主模块,并将其与html中的ng-app属性一起使用: https : //docs.angularjs.org/api / ng / directive / ngApp

This module sets up dependencies, and usually houses app config, such as routing. 该模块设置依赖关系,通常包含应用程序配置,例如路由。 Although you don't have to structure your app this way, as in the end it is just a naming convention. 尽管您不必以这种方式构建应用程序,但最终它只是一个命名约定。

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

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