由于注入错误,Angular app未运行

时间:2015-03-15 15:02:19

标签: javascript angularjs

我是角色的新手,我从样本中学习角度,但是当我使用示例代码的精确副本时,它就不起作用,为什么会这样?

<html ng-app="tw">
  <head>
    <meta charset="utf-8" />
    <title>test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="" />
    <meta name="author" content="ProteusThemes" />
  </head>

  <body> 
  <script type="text/javascript" src="angular.min.js"></script>
  <script>
  var twApp = angular.module("tw",[]);
  twApp.config(function($routeProvider){
    $routeProvider.when('/',{
    templateUrl: "templates/home.html",
    controller: "HomeController"
    })
    .otherwise({redirectTo:'/'});
  });
  twApp.controller("HomeController",['$scope',function($scope){
        alert("salam");
    }]);
  </script>
  This it simple home page
  <a href="">Home</a>
<div ng-view></div>
  </body>
</html>

当它运行时,我收到以下错误:

错误:[$ injector:modulerr] http://errors.angularjs.org/1.3.14/ $ injector / modulerr?p0 = tw&amp; p1 =%5B%24injector%3Aunpr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1。 3.14%2F%24injector%2Funpr%3Fp0%3D%2524routeProvider%0AM%2F%3C%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A6%3A417%0Aab%2FP。%24injector%3C%40http%3A %2F%2Flocalhost%2Fangular.min.js%3A38%3A7%0AD%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A36 3A13%%%0AE%40http 3A%2F%2Flocalhost%2Fangular.min.js %3A36%3A283%0AD%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A34%3A490%0Ag%2F%3C%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A35%3A117%0As %40http%3A%2F%2Flocalhost%2Fangular.min.js%3A7%3A300%0Ag%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A34%3A399%0Aab%40http%3A%2F%2Flocalhost%2Fangular .min.js%3A38%3A135%0Atc%2FD%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A17%3A381%0Atc%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A18%3A179 %0AJd%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A17%3A1%0A%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A249%3A428%0AA%40http%3A%2F%2Flocalhost %2Fangular.mi n.js%3A163%3A399%0Alf%2FC%40http%3A%2F%2Flocalhost%2Fangular.min.js%3A32%3A384%0A http://localhost/angular.min.js 第6行

2 个答案:

答案 0 :(得分:0)

您忘记添加路由js和依赖项: - &#39; ngRoute&#39;

var twApp = angular.module(&#34; tw&#34;,[&#39; ngRoute&#39;]);

添加索引: -

 <script src="angular-route.js"></script> //after downloading

更新添加了plunker

Plunker

答案 1 :(得分:0)

  1. 包含角度路由器作为脚本,其中X.Y.Z是角度版本oyu正在使用:

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js"></script>

  2. 使用$routeProvider时,将ngRoute添加为您的应用依赖:

    var twApp = angular.module("tw", ['ngRoute']);