将ngMaterial与ngRoute一起使用时出错

时间:2015-09-28 08:50:54

标签: angularjs angular-material

我正在学习AngularJS + AngularMaterial,我有一些奇怪的问题。

我想在我的项目中使用两个模块ngRoute和ngMaterial但是当我注入它们时我得到错误:“错误:[$ injector:modulerr]”。它们彼此不兼容?

示例代码可以在这里看到:http://codepen.io/anon/pen/OybZve

HTML文件

<html lang="en" ng-app="AngularClient">
  <head>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.9.4/angular-material.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=RobotoDraft:300,400,500,700,400italic">
    <meta name="viewport" content="initial-scale=1" />
  </head>
  <body layout="column" ng-controller="AppCtrl">
    <md-toolbar layout="row">
      <div class="md-toolbar-tools">
        <md-button ng-click="toggleSidenav('left')" hide-gt-sm class="md-icon-button">
          <md-icon aria-label="Menu" md-svg-icon="https://s3-us-west-2.amazonaws.com/s.cdpn.io/68133/menu.svg"></md-icon>
        </md-button>
        <h1>Hello World</h1>
      </div>
    </md-toolbar>
    <div layout="row" flex>
        <md-sidenav layout="column" class="md-sidenav-left md-whiteframe-z2" md-component-id="left" md-is-locked-open="$mdMedia('gt-sm')">
            <md-button href="#/">Home</md-button>
            <md-button href="#/projects">Projekty</md-button>
            <md-button href="#/forum">Forum</md-button>
            <md-button href="#/contact">Kontakt</md-button>
        </md-sidenav>
        <div layout="column" flex id="content">
            <md-content layout="column" flex class="md-padding">
                <ng-view></ng-view>
            </md-content>
        </div>
    </div>
    <!-- Angular Material Dependencies -->
    <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.9.4/angular-material.min.js"></script>
 </body>
</html>

JS文件

var app = angular.module('AngularClient', ['ngRoute',  'ngMaterial']);
app.config(function($routeProvider){
    $routeProvider
        .when('/', {
            templateUrl: '/static/angularclient/home.html',
            controller: 'mainController'
        })
        .when('/projects', {
            templateUrl: '/static/angularclient/projects.html',
            controller: 'projectsController'
        })
        .when('/forum', {
            templateUrl: '/static/angularclient/forum.html',
            controller: 'forumController'
        })
        .when('/contact', {
            templateUrl: '/static/angularclient/contact.html',
            controller: 'contactController'
        })
        .otherwise({
            redirectTo: '/'
        });
});

app.controller('AppCtrl', ['$scope', '$mdSidenav', function($scope, $mdSidenav){
    $scope.toggleSidenav = function(menuId) {
        $mdSidenav(menuId).toggle();
    };
}]);

app.controller('contactController', function($scope, $http){
    // Send email to site owner
    $scope.sendMessage = function(){
        $http.post('/api', {
            action: 'send_message',
            data: {
                sender: $scope.sender,
                title: $scope.topic,
                content: $scope.content
            }
        })
        .success(function(){
            console.log('udało się')
        })
        .error(function(){
            console.log('błąd :P')
        });
    }
});

1 个答案:

答案 0 :(得分:2)

如果您点击错误,角度错误页面会提到您需要使用ngRoute库,我无法在您的HTML代码中看到。

https://docs.angularjs.org/api/ngRoute