简体   繁体   English

在AngularJS中显示多个注释

[英]Display multiple comments in AngularJS

Using AngularJS and try to posting multiple comments one after another. 使用AngularJS并尝试一个接一个地发布多个注释。 i am able to post one comment but when tried again it will replace with the existing and post the new. 我可以发表评论,但再试一次,它将替换为现有评论并发布新评论。

Attached snippet as well, what I tried 附上的摘要也是我尝试过的

I want like following: 我想要以下内容:

1st comment submit : Hello !
2nd comment submit : Hi !

Result should be : 结果应为:

Hello !
Hi !

Here is my code 这是我的代码

 var myApp = angular.module('ui.bootstrap.demo', ['ui.bootstrap']); function myCtrl($scope){ $scope.MakeVisible=!$scope.MakeVisible; $scope.showAddNoteBtn=true; $scope.userText=''; $scope.Test=''; $scope.MakeVisible=false; $scope.addNoteBtnClicked=function(){ $scope.Test=''; $scope.MakeVisible=true; $scope.showAddNoteBtn=false; } $scope.cancel=function(){ $scope.MakeVisible=false; $scope.showAddNoteBtn=true; } $scope.Submit=function(){ $scope.userText=$scope.Test; $scope.MakeVisible=false; $scope.showAddNoteBtn=true; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script> <div ng-app="ui.bootstrap.demo"> <div ng-controller="myCtrl"> <h5 style="color:#287ABE;margin-bottom:10px;">{{userText}}</h5> <h5 id="label" style="color:red;margin-bottom:10px;"></h5> <div ng-hide="MakeVisible"> </div> <div ng-show="MakeVisible"> <textarea ng-model="Test"></textarea> <input type="button" value="Submit" ng-click="Submit()"/> <input type="button" value="Cancel" ng-click="cancel()"/> </div> <div> <input ng-show='showAddNoteBtn' type="button" value="Add Note" ng-click="addNoteBtnClicked()"/> </div> </div> 

you need to add the comments in to a array and iterate through the array 您需要将注释添加到数组中并遍历数组

 var myApp = angular.module('ui.bootstrap.demo', ['ui.bootstrap']); function myCtrl($scope){ $scope.MakeVisible=!$scope.MakeVisible; $scope.showAddNoteBtn=true; $scope.userText=''; $scope.Test=''; $scope.MakeVisible=false; $scope.addNoteBtnClicked=function(){ $scope.Test=''; $scope.MakeVisible=true; $scope.showAddNoteBtn=false; } $scope.cancel=function(){ $scope.MakeVisible=false; $scope.showAddNoteBtn=true; } $scope.commentArray = []; // array for storing comments $scope.Submit=function(){ $scope.commentArray.push($scope.Test); // add a comment to the array after user submit the comment $scope.MakeVisible=false; $scope.showAddNoteBtn=true; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script> <div ng-app="ui.bootstrap.demo"> <div ng-controller="myCtrl"> <h5 style="color:#287ABE;margin-bottom:10px;" ng-repeat="comment in commentArray"> {{ comment }} </h5> <h5 id="label" style="color:red;margin-bottom:10px;"></h5> <div ng-hide="MakeVisible"> </div> <div ng-show="MakeVisible"> <textarea ng-model="Test"></textarea> <input type="button" value="Submit" ng-click="Submit()"/> <input type="button" value="Cancel" ng-click="cancel()"/> </div> <div> <input ng-show='showAddNoteBtn' type="button" value="Add Note" ng-click="addNoteBtnClicked()"/> </div> </div> 

You use arrays and ngRepeat directive to achieve it, like in this code snippet below: 您可以使用数组ngRepeat指令来实现它,如下面的代码片段所示:

 var myApp = angular.module('ui.bootstrap.demo', ['ui.bootstrap']); function myCtrl($scope){ $scope.MakeVisible=!$scope.MakeVisible; $scope.showAddNoteBtn=true; $scope.userText=[]; $scope.Test=''; $scope.MakeVisible=false; $scope.addNoteBtnClicked=function(){ $scope.Test=''; $scope.MakeVisible=true; $scope.showAddNoteBtn=false; } $scope.cancel=function(){ $scope.MakeVisible=false; $scope.showAddNoteBtn=true; } $scope.Submit=function(){ $scope.userText.push($scope.Test); $scope.MakeVisible=false; $scope.showAddNoteBtn=true; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script> <div ng-app="ui.bootstrap.demo"> <div ng-controller="myCtrl"> <h5 style="color:#287ABE;margin-bottom:10px;" ng-repeat="t in userText">{{t}}</h5> <h5 id="label" style="color:red;margin-bottom:10px;"></h5> <div ng-hide="MakeVisible"> </div> <div ng-show="MakeVisible"> <textarea ng-model="Test"></textarea> <input type="button" value="Submit" ng-click="Submit()"/> <input type="button" value="Cancel" ng-click="cancel()"/> </div> <div> <input ng-show='showAddNoteBtn' type="button" value="Add Note" ng-click="addNoteBtnClicked()"/> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM