使用ng-view时如何避免包含所有.js和.css文件?

时间:2014-09-21 19:06:16

标签: angularjs ng-view

我们如何避免在index.html中包含所有.js和.css文件,如下例所示:

<!DOCTYPE html>
<html ng-app="MyApp">
<head>
  <base href="/">
  <meta charset="utf-8">
  <title>ShowTrackr</title>
  <link href="favicon.png" rel="icon" type="image/png">
  <link href='http://fonts.googleapis.com/css?family=Roboto|Montserrat:400,700|Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
  <link href="//cdn.jsdelivr.net/ionicons/1.4.1/css/ionicons.min.css" rel="stylesheet">
  <link href="stylesheets/animate.css" rel="stylesheet">
  <link href="stylesheets/style.css" rel="stylesheet">
</head>
<body>
<div ng-controller="NavbarCtrl" class="navbar navbar-default navbar-static-top" role="navigation" bs-navbar>
  <div class="navbar-header">
    <a class="navbar-brand" href="/">
      <span>Show<strong>Trackr</strong></span>
    </a>
  </div>
  <ul class="nav navbar-nav">
    <li data-match-route="/$"><a href="/">Home</a></li>
    <li data-match-route="/add"><a href="/add">Add</a></li>
  </ul>
  <ul class="nav navbar-nav pull-right" ng-if="!currentUser">
    <li data-match-route="/login"><a href="/login">Login</a></li>
    <li data-match-route="/signup"><a href="/signup">Sign up</a></li>
  </ul>
  <ul class="nav navbar-nav pull-right" ng-if="currentUser">
    <li class="navbar-text" ng-bind="currentUser.email"></li>
    <li><a href="javascript:void(0)" ng-click="logout()">Logout</a></li>
  </ul>
</div>

<div ng-view class="{{pageClass}}"></div>

<script src="vendor/angular.js"></script>
<script src="vendor/angular-strap.js"></script>
<script src="vendor/angular-strap.tpl.js"></script>
<script src="vendor/angular-messages.js"></script>
<script src="vendor/angular-resource.js"></script>
<script src="vendor/angular-route.js"></script>
<script src="vendor/angular-animate.js"></script>
<script src="vendor/moment.min.js"></script>

<script src="app.js"></script>
<script src="filters/fromNow.js"></script>
<script src="directives/uniqueEmail.js"></script>
<script src="directives/passwordStrength.js"></script>
<script src="controllers/main.js"></script>
<script src="controllers/detail.js"></script>
<script src="controllers/add.js"></script>
<script src="controllers/navbar.js"></script>
<script src="controllers/login.js"></script>
<script src="controllers/signup.js"></script>
<script src="services/auth.js"></script>
<script src="services/show.js"></script>
<script src="services/subscription.js"></script>

<!--<script src="app.min.js"></script>-->
<!--<script src="templates.js"></script>-->
</body>
</html>

此代码来自此github:https://github.com/sahat/tvshow-tracker

该代码用于教程:http://sahatyalkabov.com/create-a-tv-show-tracker-using-angularjs-nodejs-and-mongodb/

我想做点什么:

angular.module("MyApp", ["ngRoute"])
    .config(["$routeProvider", function($routeProvider) {
        $routeProvider
        .when("/", {
            templateUrl: "views/home.html",
            controller: "homeCtrl"
            /*
               Here say to include in the same time specific .js
               (for example the one who contain the 'homeCtrl' controller) and .css
            */
        });
    }]);

因为使用上面的html代码,会有很多服务器请求并且可能无用(如果用户不使用整个应用程序)。我无法想象谷歌没有想到它。

1 个答案:

答案 0 :(得分:0)

您可以在视图中内联CSS和JS代码。如果视图是可缓存的,那么这不是一个大问题。

您可能需要查看http://webcomponents.org

http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom/