Angular JS - 基本数据绑定无法正常工作

时间:2022-01-07 13:09:45

i'm new to angular, so started with very basic example, but this is also not working. so please help me out. below code should write the content of textbox to the page.

我是角色的新手,所以从非常基本的例子开始,但这也没有用。所以请帮帮我。下面的代码应该将文本框的内容写入页面。

<html xmlns="http://www.w3.org/1999/xhtml">
<head ng-app>
    <title></title>
</head>
<body>
    <div class="container">
        Name: <input type="text" ng-model="name" />{{name}}
    </div>
    <script src="/script/angular.js" type="text/javascript"></script>
</body>
</html>

4 个解决方案

#1


1  

ng-app should be inside html or body, inside whichever tags you intend to use angular.

ng-app应该在html或body中,在你打算使用angular的任何标签内。

#2


1  

In your code ng-app scope is ending with in head tags only. You haven't used ng-app at root level. You can use it at html or body or main div tag level. And there is no ng-controller in your markup. Of course you can also configure controller through js file also. You use below code.

在您的代码中,ng-app范围仅以head标记结尾。您尚未在根级别使用ng-app。您可以在html或body或main div标签级别使用它。并且标记中没有ng-controller。当然你也可以通过js文件配置控制器。你使用下面的代码。

HTML

<html ng-app="plunker">

<head>
  <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14">    </script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <div class="container">
      Name: <input type="text" ng-model="name" />{{name}}
  </div>
</body>

</html>

JS

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

#3


0  

ng-app should be in html tag, not head.

ng-app应该是html标签,而不是头部。

#4


0  

put ng-app in tag, not in head tag

将ng-app放在标签中,而不是在head标签中

#1


1  

ng-app should be inside html or body, inside whichever tags you intend to use angular.

ng-app应该在html或body中,在你打算使用angular的任何标签内。

#2


1  

In your code ng-app scope is ending with in head tags only. You haven't used ng-app at root level. You can use it at html or body or main div tag level. And there is no ng-controller in your markup. Of course you can also configure controller through js file also. You use below code.

在您的代码中,ng-app范围仅以head标记结尾。您尚未在根级别使用ng-app。您可以在html或body或main div标签级别使用它。并且标记中没有ng-controller。当然你也可以通过js文件配置控制器。你使用下面的代码。

HTML

<html ng-app="plunker">

<head>
  <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14">    </script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <div class="container">
      Name: <input type="text" ng-model="name" />{{name}}
  </div>
</body>

</html>

JS

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

#3


0  

ng-app should be in html tag, not head.

ng-app应该是html标签,而不是头部。

#4


0  

put ng-app in tag, not in head tag

将ng-app放在标签中,而不是在head标签中