2015-05-14 51 views
1

Q]我是基本的開發人員,如何使用angular-js顯示文本從textarea提交按鈕?從textarea顯示文本提交按鈕 - Angular JS

我有當前的代碼以我: - 從文本區域

<html ng-app="myApp"> 

<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
    <script> 
    myApp = angular.module('myApp', []); 

    function Ctrl($scope) { 
     $scope.list = []; 
     $scope.pass = $scope.list; 
     $scope.text = 'Share your knowledge !'; 
     $scope.submit = function() { 
      if ($scope.text) { 
       $scope.pass.push($scope.text); 
      } 
     }; 
    } 
    </script> 
</head> 

<body> 
    <div ng-app> 
     <div ng-controller="Ctrl"> 
      <form ng-submit="submit()"> 
       <br> 
       <textarea style="resize:initial;" type="" ng-model="text" name="text"></textarea> 
       <br> 
       <input align="right" style="margin-top:20px;margin-left:120px; align:right;" type="submit" id="submit" value="Submit" /> 
       <pre>{{list}}</pre> 

      </form> 
     </div> 
    </div> 
</body> 

</html> 

上面的代碼只是顯示的消息以陣列形式。

但我只想要打印/顯示單個文本消息。這怎麼能實現?謝謝 。

+0

你是什麼意思_single text message_。 ?你能展示你的預期產出嗎? – Zee

+0

這是因爲你正在將文本推入數組 - >'$ scope.pass.push'。要獲取一個字符串,請執行'$ scope.pass.join('');' – lshettyl

+0

我只想在點擊提交按鈕後顯示一條消息,而不是以數組格式顯示。 – Vikrant

回答

0

我想你只想顯示當前在textarea中的消息。如果是這樣的話:

function Ctrl($scope) { 
     $scope.pass = []; 
     $scope.text = 'Share your knowledge !'; 
     $scope.submit = function() { 
      if ($scope.text) { 
       $scope.pass.push($scope.text); 
       $scope.list = $scope.text; 
      } 
     }; 
    } 
+0

是的,即使你的回答正在工作:) – Vikrant

0

您可以將用戶text作爲textarea的值。 當從提交時設置textarea的值。

我想你希望textarea中的消息在表單提交後顯示在list中。

<html ng-app="myApp"> 

<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
    <script> 
    myApp = angular.module('myApp', []); 

    function Ctrl($scope) { 
     $scope.list = ''; 
     // ^^^^^^^^^^^^^ 

     $scope.pass = $scope.list; 
     $scope.text = 'Share your knowledge !'; 

     $scope.submit = function() { 
      $scope.list = $scope.text; 
      // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
     }; 
    } 
    </script> 
</head> 

<body> 
    <div> 
     <div ng-controller="Ctrl"> 
      <form ng-submit="submit()"> 
       <br> 
       <textarea style="resize:initial;" type="" ng-model="text" name="text"></textarea> 
       <br> 
       <input align="right" style="margin-top:20px;margin-left:120px; align:right;" type="submit" id="submit" value="Submit" /> 
       <pre>{{list}}</pre> 
      </form> 
     </div> 
    </div> 
</body> 

</html> 
+0

對不起,你沒有得到我的問題。儘管我的錯誤...我不想提交按鈕上的文本,但文本應該在屏幕上顯示onclick提交功能,而不顯示在數組格式。 – Vikrant

+0

@vikrant在'list'中? – Tushar

+0

是的,只是想在點擊提交按鈕後顯示一條消息,而不是在數組中。 – Vikrant