我正在离线学习代码学院的 angularjs 初始设置教程。原因是我可以将其用作我自己的应用程序的指南。我有 3 个文件 MainController.js、app.js 和 index.html。
应用程序.js
var app = angular.module('myApp', []);
主 Controller .js
app.controller('MainController', ['$scope', function($scope) {
$scope.title = 'Top Sellers in Books';
}]);
index.html
<!doctype html>
<html ng-app>
<head>
<title>My App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div class="main" ng-controller="MainController">
{{ title }}
</div>
<!-- Modules -->
<script src="js/app.js"></script>
<!-- Controllers -->
<script src="js/controllers/MainController.js"></script>
</body>
</html>
在控制台中我收到此错误并且无法打印 $scope.title
我似乎无法弄清楚为什么会产生这个错误
最佳答案
问题出在你的html
上代码。您已初始化 ng-app
两次,一次在<html>
一次在 <body>
标签。删除 <html>
中的那个标签。您的 html 代码应如下所示,
<!doctype html>
<html> <!-- Remove `ng-app` here -->
<head>
<title>My App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"> </script>
</head>
<body ng-app="myApp">
<div class="main" ng-controller="MainController">
{{ title }}
</div>
<!-- Modules -->
<script src="js/app.js"></script>
<!-- Controllers -->
<script src="js/controllers/MainController.js"></script>
</body>
</html>
关于javascript - 设置 Angular JS 奇怪的错误 angularjs :12416,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625989/