我的 script.js 文件中有这段代码
var mainController = function($scope){
$scope.message = "Plunker";
};
这是我的 HTML
<!DOCTYPE html>
<html ng-app>
<head>
<script data-require="[email protected]" data-semver="1.6.1" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="mainController">
<h1>Hello {{ message }}</h1>
</body>
</html>
我在开始的 html 标签中声明了 ng-app
但我在控制台上收到此错误,表明 mainController 未注册
转述一下http://www.w3schools.com/angular/angular_modules.asp
<div ng-app="myApp" ng-controller="mainController">
{{ firstName + " " + lastName }}
</div>
<script>
var app = angular.module("myApp", []);
app.controller("mainController", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
重要的一行是
app.controller("mainController", function($scope)
它将您的控制器注入您的应用程序
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)