为什么AngularJs仅在引用ng-app和ng-controller

时间:2018-02-23 01:19:42

标签: angularjs

为什么我在控制台中遇到错误:

  <!DOCTYPE html>
  <html ng-app="MyApp" ng-controller="myCtrl">
  <head>
      <meta charset="utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>Page Title</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script>
  </head>
  <body>

  </body>
  </html>
  

未捕获错误:[$ injector:modulerr]   http://errors.angularjs.org/1.4.3/ $注射器/ modulerr P0 = MyApp的&安培; P1 =错误%3A%20%5B%24injector%3Anomod%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.3%2F%24injector% 2Fnomod%3Fp0%3DMyApp%0A%20%20%20%20原子%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A6%3A416% 0A%20%20%20%20原子%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A24 3A66%%0A%20%20% 20%20原子%20A 20%(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A23%3A109)%0A%20%20%20 %20AT%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A23%3A352%0A%20%20%20%20原子%20https%3A %2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A37%3A381%0A%20%20%20%20原子%20米%20(HTTPS%3A%2F %2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A7%3A322)%0A%20%20%20%20原子%20克%20(HTTPS%3A%2F% 2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A37%3A229)%0A%20%20%2 0%20原子%20EB%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A40%3A503)%0A%20%20%20 %20AT%20D%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A19%3A339)%0A%20%20%20% 20AT%器20Ac%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.4.3%2Fangular.min.js%3A20%3A151)       在angular.js:38       在angular.js:4385       在m(angular.js:336)       在g(angular.js:4346)       在eb(angular.js:4272)       在d(angular.js:1630)       在Ac(angular.js:1651)       在Zd(angular.js:1545)       在angular.js:28359       在HTMLDocument.a(angular.js:2996)

2 个答案:

答案 0 :(得分:1)

您需要在脚本或单独的文件中声明模块和控制器

<!DOCTYPE html>
  <html ng-app="MyApp" ng-controller="myCtrl">
  <head>
      <meta charset="utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>Page Title</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script>
 </head>
 <body>
 <h1>{{text}}</h1>
 <script>
   var app = angular.module('MyApp',[]);
   app.controller('myCtrl',function($scope){
       $scope.text = "Works";
   });
 </script>
 </body>
 </html>

答案 1 :(得分:1)

因为您尚未定义“MyApp”或“MyCtrl”。它们需要在网页上包含的js文件中定义。我建议你阅读一个关于设置AngularJs应用程序的基础教程。