AngularJS:导航栏

时间:2016-11-16 18:05:31

标签: javascript angularjs navigation

我正在尝试实施此示例:https://material.angularjs.org/1.1.1/demo/navBar

所以我创建了以下index.html

<!DOCTYPE html> <html lang="en"> 
    <head>
        <meta charset="UTF-8">
    </head> 
    <body> 
    <div ng-controller="AppCtrl" ng-cloak="" class="navBardemoBasicUsage" ng-app="MyApp">
    <md-content class="md-padding">
        <md-nav-bar md-selected-nav-item="currentNavItem" nav-bar-aria-label="navigation links">
            <md-nav-item md-nav-click="goto('page1')" name="page1">Page One</md-nav-item>
            <md-nav-item md-nav-click="goto('page2')" name="page2">Page Two</md-nav-item>
            <md-nav-item md-nav-click="goto('page3')" name="page3">Page Three</md-nav-item>
        </md-nav-bar>
        <div class="ext-content">
            External content for `<span>{{currentNavItem}}</span>`
        </div>
    </md-content></div>

    <script src="vendor/angular/angular.min.js"></script> 
    <script src="http://ngmaterial.assets.s3.amazonaws.com/svg-assets-cache.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.min.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-aria.min.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-messages.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-route.min.js"></script> 
    <script src="app.js"></script> 
</body> 
</html>

并关注app.js

(function() {
    'use strict';

    angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
        .controller('AppCtrl', AppCtrl);

    function AppCtrl($scope) {
        $scope.currentNavItem = 'page1';
    }
})();

我得到了这个例外:

Failed to instantiate module MyApp due to:
Error: [$injector:modulerr] http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=n...)
    at http://localhost:8000/vendor/angular/angular.min.js:6:412
    at http://localhost:8000/vendor/angular/angular.min.js:40:222
    at q (http://localhost:8000/vendor/angular/angular.min.js:7:355)
    at g (http://localhost:8000/vendor/angular/angular.min.js:39:319)
    at http://localhost:8000/vendor/angular/angular.min.js:39:488
    at q (http://localhost:8000/vendor/angular/angular.min.js:7:355)
    at g (http://localhost:8000/vendor/angular/angular.min.js:39:319)
    at cb (http://localhost:8000/vendor/angular/angular.min.js:43:336)
    at c (http://localhost:8000/vendor/angular/angular.min.js:20:390)
    at Bc (http://localhost:8000/vendor/angular/angular.min.js:21:179

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您缺少angular-material.js和css

的参考
      <link rel="stylesheet" href="//rawgit.com/angular/bower-material/master/angular-material.css" />
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-animate.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-aria.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angular_material/0.11.4/angular-material.min.js"></script>

<强> DEMO

答案 1 :(得分:0)

我认为你应该将你的ng-app放在body标签中,就像

一样
<body ng-app="MyApp">

正如文档所指出的那样,你不应该将你的app和其他指令放在同一个标​​签中

https://docs.angularjs.org/api/ng/directive/ngApp

希望它有所帮助。