2017-01-04 27 views
0

在這裏我有代碼在textarea中使用ng模型。我想獲得textarea的值,並添加一些靜態內容。然後添加到ng模型中。但它不起作用。 在這裏,我貼我的代碼: 在HTML:角js吳模型價值不工作在textarea

<textarea ng-model="user_text_comment" rows="5" Placeholder="Enter your comments" required="required"></textarea> 
<button ng-click="addcardToComments(user_text_comment)"></button> 

在控制器:

scope.user_text_comment = ""; 
$scope.addcardToComments = function(usercomment){ 
var addComments = "This is an appended text"; 
$scope.user_text_comment =usercomment.toString()+''+addComments; 
console.log($scope.user_text_comment); 
}); 

這裏CONSOLE.LOG打印修正值。但它在textarea中沒有改變。

+0

你定義一些父標籤控制器? –

+0

你能提供一個小提琴嗎? –

+0

控制器的第1行可能是'$ scope.user_text_comment =「」;'而不是'scope.user_text_comment =「」;' –

回答

-1

在第1行添加$ scope.user_text_comment =「」; ($ scope.user_text_comment =「」;)

並刪除toString()的textarea,我認爲不需要這個選項。

謝謝..

+0

如果您不清楚您的答案是否有效,請發表評論。 – 31piy

+0

我改變它..然後也沒有工作。 – SARAN

1

這是一個小的語法錯誤,你有。

$scope.addcardToComments = function(usercomment){ 
    var addComments = "This is an appended text"; 
    $scope.user_text_comment =usercomment.toString()+''+addComments; 
    console.log($scope.user_text_comment); 
}); 

額外的 「」 不是必需的。

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

 
app.controller('ctrl', function($scope) { 
 
    $scope.user_text_comment = ""; 
 
    $scope.addcardToComments = function(usercomment) { 
 
    var addComments = "This is an appended text"; 
 
    $scope.user_text_comment = usercomment.toString() + '' + addComments; 
 
    alert($scope.user_text_comment); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app="myApp" ng-controller="ctrl"> 
 
    <p>Hello {{name}}!</p> 
 
    <textarea ng-model="user_text_comment" rows="5" Placeholder="Enter your comments" required="required" style="float:left;"></textarea> 
 
    <button ng-click="addcardToComments(user_text_comment)" style="float:left;">Save</button> 
 
</body>