angular.js文件中的输出不正确

时间:2015-06-05 06:21:40

标签: angularjs

我写了这段代码

   <html ng-app>
   <head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
<script>
angular.module('scopeExample', [])
.controller('MyController', ['$scope', function($scope) {
   $scope.username = 'World';

   $scope.sayHello = function() {
   $scope.greeting = 'Hello ' + $scope.username + '!';
 };
}]); 
</script>
</head>
<body>
<div ng-controller="MyController" >
 Your name:
<input type="text" ng-model="username">
<button ng-click='sayHello()'>greet</button>
<hr>
 {{greeting}}
 </div>
</body>
</html>

代替从文本框中获取输入,它只是打印{{greeting}},甚至文本框也没有用world初始化。

4 个答案:

答案 0 :(得分:1)

设置在顶部的html标记ng-app ="scopeExample"

答案 1 :(得分:0)

定义你的ng-app =“scopeExample”,Fiddle

angular.module('scopeExample', [])
.controller('MyController', ['$scope', function($scope) {
   $scope.username = 'World';

   $scope.sayHello = function() {
   $scope.greeting = 'Hello ' + $scope.username + '!';
 };
}]); 

答案 2 :(得分:0)

缺少应用标记模块名称,您可以在HTML中添加ng-app =“scopeExample”。

<html ng-app="scopeExample">

需要自举角度 参考: - https://docs.angularjs.org/api/ng/directive/ngApp

答案 3 :(得分:0)

function HelloCntl($area) {
    $area.test= 'Demo';
}
<div ng-controller="MyController">
  Your name: <input type="text" ng-model="test"/>
  <hr/>
  Hello {{test|| "Demo"}}!
</div>

您可以尝试此代码我检查此代码是否正常工作,我也在使用此代码