为什么我的Html文件无法识别AngularJS变量

时间:2017-04-08 11:32:17

标签: html angularjs

<!DOCTYPE html>
<html ng-app="">
<head>
    <title>hello</title>
    <script type="text/javascript" src="lib/angular.js"></script>
</head>
<body>
	<input type="text" ng-model="name">
	<p>{{name}} </p>
</html>

我尝试做一个非常简单的角度项目,但有一些错误。在输出页面中出现变量名称,而不是预期的值。

2 个答案:

答案 0 :(得分:0)

您需要声明模块和控制器,代码中缺少这两个,

var app = angular.module("myApp", []);
    app.controller("myCtrl", function($scope) {

    });

<强>样本

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
	<input type="text" ng-model="name">
	<p>{{name}} </p>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  
});
</script>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

检查样本angularjs代码

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.name = "Hello";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="myCtrl">
    <input type="text" ng-model="name">
    <p>{{name}} </p>
  </div>
</body>