Angular JS的基本问题

Basic Issue with Angular JS

本文关键字:问题 JS Angular      更新时间:2023-09-26

我是angular js的新手,从一些基本的例子开始。但我被卡住了。下面是我的JS代码(openHtml.JS)。

(function(){
    var app = angular.module("MyApp",[]);
    app.controller("MyAppController",function($scope){
      $scope.message = "It is a message";
    });
}());

我的HTML如下

<html>
<head>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyAppController">
        <label>{{messsage}}</label>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src="openHtml.js"></script>
</body>
</html>

但是该消息并没有显示在html中。知道为什么吗?

Jfiddle示例:http://jsfiddle.net/Lx7fuchn/

您在HTML中拼错了"message"(一个"s"太多")

<div ng-app="MyApp" ng-controller="MyAppController">
    <label>{{message}}</label>
</div>

您在html模板中放入3个"S"。。。尝试使用2,就像在你的控制器中一样:p

$scope.message = "It is a message"; 
<label>{{messsage}}</label>
<div ng-app="MyApp" ng-controller="MyAppController">
        <label>{{message}}</label>
    </div>

错误的表达式{{messsage}}

你也可以简单地写你的模块和控制器:

angular.module("MyApp",[])
.controller("MyAppController",function($scope){
  $scope.message = "It is a message";
});