为什么ng-bind无法使用ng-app

时间:2016-04-05 08:36:39

标签: angularjs

我正在学习angularjs。当我尝试使用ng-app进行ng-bind时,它无法正常工作。 当我像ng-app =""那样保持空虚时,它起作用了。 谁能解释一下呢?

<!doctype html >
<html lang="en" >
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script>

  </script>
 </head>
     <body>

        <div ng-app = "">
         <p>Enter your Name: <input type = "text" ng-model = "name"></p>
         <p>Hello <span ng-bind = "name"></span>!</p>
        </div>


     </body>
</html>

2 个答案:

答案 0 :(得分:1)

要使用ng-app,您需要先为其命名<div ng-app = "MyApp">

最后添加一些脚本:

<script type="text/javascript">
  angular.module('MyApp', []);
</script>

然后中提琴!

样品:

<!doctype html >
<html lang="en" >
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script>

  </script>
 </head>
     <body>

        <div ng-app = "MyApp">
         <p>Enter your Name: <input type = "text" ng-model = "name"></p>
         <p>Hello <span ng-bind = "name"></span>!</p>
        </div>


     </body>

  <script type="text/javascript">
    angular.module('MyApp', []);
  </script>
</html>

答案 1 :(得分:0)

  • 您忘了添加ng-init="name='John Doe'"