2014-10-22 46 views
1

我有以下的html:數據綁定不是在我的AngularJS應用工作

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Basic</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js type="text/javascript"> 
     </script> 
    </head> 
    <body> 
     <div ng-app="myApp"> 
      <input ng-model="to" type="email" 
       placeholder="Recipient here.."/> 
      <textarea ng-model="emailBody" 
       placeholder="Email body here.."> 
      </textarea> 
      <h2> {{ emailBody }} </h2> 
     </div> 
    </body> 
</html> 

我從TextArea引用emailBody數據,但它不綁定相關的數據。它只是從字面上輸入{{emailBody}}。

我在做什麼錯?

+0

javascript在哪裏? – for3st 2014-10-22 01:44:45

回答

4

假設您第一次使用Angular,您可能需要使用不帶值的「ng-app」參數,該參數將設置注入上下文而不將其映射到指定的應用程序控制器(在您的例)。還請注意,您缺少腳本標記的src參數的結束引號。

以下是您的示例,這兩個修補程序的工作方式與預期的一樣。

<!DOCTYPE HTML> 
<html ng-app> 
    <head> 
     <title>Basic</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js" type="text/javascript"></script> 
    </head> 
    <body> 
     <div> 
      <input ng-model="to" type="email" placeholder="Recipient here.."/> 
      <textarea ng-model="emailBody" placeholder="Email body here.."> 
      </textarea> 
      <h2> {{emailBody}} </h2> 
     </div> 
    </body> 
</html> 
+0

是的,當場..,正確提及OP的問題是,其餘OP可以從文檔和其他東西:) +1 http://plnkr.co/edit/V2Mppb?p=preview – PSL 2014-10-22 02:04:19

1

嘗試將以下內容添加到java腳本文件中。

angular.module('myApp',[]).controller('myController',['$scope',function($scope){ 
    $scope.emailBody = 'test'; 
}]); 

它定義了應用程序/模塊和要綁定到emailBody字段。

然後,您可以添加腳本的引用,並添加ng-contoller來引用控制器。

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Basic</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js" type="text/javascript"> 
     </script> 
     <script src='someUrlToYour JavaScript file'></script> 
    </head> 
    <body> 
     <div ng-app="myApp" ng-controller='myController'> 
      <input ng-model="to" type="email" 
       placeholder="Recipient here.."/> 
      <textarea ng-model="emailBody" 
       placeholder="Email body here.."> 
      </textarea> 
      <h2> {{ emailBody }} </h2> 
     </div> 
    </body> 
</html> 
+0

'我在做什麼錯了嗎?< - 我真的懷疑這真的是OP要求的.. :( – PSL 2014-10-22 02:07:22