角js 1.3.14的问题

时间:2015-10-20 16:29:45

标签: javascript angularjs

我是一名UI开发人员,也是角度js的新手。 我在尝试使用angular js在输入类型文本中输入时更新

Hello {{name}}

中的值,但是如果不指定控制器,我将无法执行此操作。 p>

任何人都可以帮助我。

提前感谢您的回复。

以下是代码:

<!DOCTYPE html>
<html ng-app="myapp">
<head>
    <title>AngularJS First Application</title>
  <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h1>Sample Application</h1>
<p> Enter your Name <input  type ="text"  ng-model ="name"></p>
<p> Hello {{name}} ! </p>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

看看我在这里所做的更改。基本上你需要实际运行角度,这样才能发挥它的魔力

angular.module('myapp', [])
  .controller('myctrl', function() {
});
<!DOCTYPE html>
<html ng-app="myapp">

<head>
  <title>AngularJS First Application</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>

<body ng-controller='myctrl'>
  <h1>Sample Application</h1>
  <p>Enter your Name
    <input type="text" ng-model="name">
  </p>
  <p>Hello {{name}} !</p>
</body>

</html>

答案 1 :(得分:0)

为了扩展Simon的回答并回答你的问题,如果没有控制器(&#34; ng-controller&#34;),这是不可行的。控制器告诉Angular在哪里以及如何做它的魔力。在最基本的示例中(如此),您在HTML和Javascript中声明控制器,Angular将它们绑定在一起,允许您在两者之间来回无缝地共享数据。

&#13;
&#13;
angular.module('myapp', [])
  .controller('myctrl', function($scope) {
      // This is where you can program logic for the scope of your controller 'myctrl'
      // For example, I could change the name here instead like this:
      $scope.name="My name defined in controller";

      // Or perhaps you want to create a function that is called upon the click of a button:
      $scope.changeName = function(newName){
          $scope.name = newName;
      }
});
&#13;
<!DOCTYPE html>
<html ng-app="myapp">

<head>
  <title>AngularJS First Application</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>

<body ng-controller='myctrl'> 
  <!-- Begin scope -->
  <h1>Sample Application</h1>
  <p>Enter your Name
    <input type="text" ng-model="name">
  </p>
  <p>Hello {{name}} !</p>

  <!-- Change name to "Name#2" when you click the button -->
  <input type="submit" ng-click="changeName('Name#2')"/>
  <!-- End scope -->
</body>
<!-- So if you tried to access the 'name' variable out here you wouldn't
     be able to because it is outside the scope of your controller -->  
</html>
&#13;
&#13;
&#13;