Angularjs Controller未在视图中显示

时间:2018-03-28 00:54:35

标签: angularjs controller

我一直在为Angular上网,我无法弄清楚为什么我的信息不会在视图中显示。我已从全局命名空间中删除了控制器。有人可以帮忙吗?

`<!DOCTYPE html>
 <html ng-app="app">
 <head>
 <meta charset="utf-8" />
 <script data-require="angular.js@1.6.1" data-semver="1.6.1" 
  src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"> 
 </script>
 <script src="Script1.js"></script>


<title></title>
</head>
<body ng-controller="mainController">
<h1>Hello {{ message }}</h1>
</body>
</html>

var app = angular.module('myApp', []);
app.controller('mainController', function ($scope) {
$scope.message = "Hello Again";

});`

2 个答案:

答案 0 :(得分:0)

您的ng-app名称有误,请将其更改为

<html ng-app="myApp">

<强>样本

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('mainController', function($scope) {
$scope.message = "Hello Again";
});
&#13;
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <head>
<title></title>
</head>
<body ng-controller="mainController">
<h1>Hello {{ message }}</h1>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您必须编写您在js文件中使用的相同应用名称。请将您的ng-app名称更改为&#39; myApp&#39;

var app = angular.module('myApp', []);

<html ng-app="app"> // change this app to myApp.