AngularJS的单页应用程序-如何建模

Single Page Apps with AngularJS - How to model?

本文关键字:建模 何建模 单页 应用程序 AngularJS      更新时间:2023-09-26

我基于本教程构建了一个单页应用程序:

https://scotch.io/tutorials/single-page-apps-with-angularjs-routing-and-templating

这就是他们建议的文件结构:

 - script.js             <!-- stores all our angular code -->
    - index.html            <!-- main layout -->
    - pages                 <!-- the pages that will be injected into the main layout -->
    ----- home.html
    ----- about.html
    ----- contact.html

关于html页面的部分非常简单——每个ng视图都有一个页面。凉的

但是script.js在我看来是一个糟糕的模型。

我真的应该把所有的js代码放在一个文件里吗?

在我看来,这不是最好的建模方法。

如果我的单页应用程序中有很多页面,会发生什么?

我会有一个很长的一个js文件,里面有所有的控制器。。

在angularjs中建模单页应用程序的最佳实践是什么?

谢谢!

// script.js
    // create the module and name it scotchApp
        // also include ngRoute for all our routing needs
    var scotchApp = angular.module('scotchApp', ['ngRoute']);
    // configure our routes
    scotchApp.config(function($routeProvider) {
        $routeProvider
            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })
            // route for the about page
            .when('/about', {
                templateUrl : 'pages/about.html',
                controller  : 'aboutController'
            })
            // route for the contact page
            .when('/contact', {
                templateUrl : 'pages/contact.html',
                controller  : 'contactController'
            });
    });
    // create the controller and inject Angular's $scope
    scotchApp.controller('mainController', function($scope) {
        // create a message to display in our view
        $scope.message = 'Everyone come and see how good I look!';
    });
    scotchApp.controller('aboutController', function($scope) {
        $scope.message = 'Look! I am an about page.';
    });
    scotchApp.controller('contactController', function($scope) {
        $scope.message = 'Contact us! JK. This is just a demo.';
    });

本详尽的样式指南建议您根据功能创建文件夹和组织应用程序。https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#application-结构

我建议阅读整个"应用程序结构"部分,它确实帮助我组织了我的角度项目