AngularJS:textarea和ng-repeat-ed输入之间的双向绑定

前端之家收集整理的这篇文章主要介绍了AngularJS:textarea和ng-repeat-ed输入之间的双向绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我打算问这个问题,但我想出了一个解决方案.所以在这一点上,我正在寻找对我的解决方案的批评.

>我有一个静态textarea,以及一个带有ng-repeat指令的输入.
>当用户在textarea中键入一个句子时,将为句子中的每个单词创建一个输入.
>然后,如果用户更新任何输入中的文本,则更新textarea语句中的相应单词(实际上重新创建整个句子).

演示:http://plnkr.co/edit/bSjtOK?p=preview

问题

请记住,我只有两周时间进入AngularJS学习:

>我是以“棱角分明”的方式写的吗?
>有什么我可以做得更好吗?
>我是否违反任何禁忌?

缩写代码

HTML

  1. <textarea ng-model="sentence" ng-change="parseSentence()" style="width: 100%; height: 15em;"></textarea>
  2.  
  3. <input type="text" ng-repeat="w in words" ng-model="w.word" ng-change="buildSentance(w)" />

JavaScript的

  1. function WordCtrl($scope,debounce) {
  2.  
  3. $scope.words = [];
  4. $scope.sentence = 'Hello there how are you today?';
  5.  
  6. // this is called when the textarea is changed
  7. // it splits up the textarea's text and updates $scope.words
  8. $scope.parseSentence = function() {
  9.  
  10. var words = $scope.sentence.split(/\s+/g);
  11. var wordObjects = [];
  12.  
  13. for (var i=0;i<words.length;i++) {
  14. wordObjects.push({word: words[i]});
  15. }
  16.  
  17. if ((words.length == 1) && (words[0] === '')) {
  18. $scope.words = [];
  19. } else {
  20. $scope.words = wordObjects;
  21. }
  22.  
  23. };
  24.  
  25. $scope.parseSentenceDebounced = debounce($scope.parseSentence,1000,false);
  26.  
  27. $scope.buildSentance = function(w) {
  28.  
  29. var words = [];
  30.  
  31. for (var i=0;i<$scope.words.length;i++) {
  32. var word = $scope.words[i].word;
  33. if (word.replace(/\s+/g,'') !== '') {
  34. words.push(word);
  35. }
  36. }
  37.  
  38. $scope.sentence = words.join(' ');
  39.  
  40. // if the user puts a space in the input
  41. // call parseSentence() to update $scope.words
  42. if (w.word.indexOf(' ') > -1) {
  43. $scope.parseSentenceDebounced();
  44. }
  45.  
  46. }
  47.  
  48. $scope.parseSentence();
  49.  
  50. }
你有趣的问题.我把你的代码放在我的页面上,我注意到的第一件事就是你无法在控制器方法中通过debounce.

下一个问题我注意到你有一个ng-change,用ng-change改变另一个盒子上的值.我将事件更改为Keypress以停止摘要中的摘要.

这是在JSFiddle enter link description here中工作

代码

HTML

  1. <body ng-app="portal">
  2. <div ng-controller="WordCtrl">
  3. <textarea ng-model="sentence" ng-keypress="parseSentence()" style="width: 100%; height: 15em;"></textarea>
  4. <input type="text" ng-repeat="w in words" ng-model="w.word" ng-keypress="buildSentance(w)" />
  5. </div>
  6. </body>

使用Javascript

  1. angular.module("portal",[]).controller("WordCtrl",function($scope) {
  2. $scope.words = [];
  3. $scope.sentence = 'Hello there how are you today?';
  4. $scope.parseSentence = function () {
  5. var words = $scope.sentence.split(/\s+/g);
  6. var wordObjects = [];
  7. for (var i = 0; i < words.length; i++) {
  8. wordObjects.push({ word: words[i] });
  9. }
  10. if ((words.length == 1) && (words[0] === ''))
  11. {
  12. $scope.words = [];
  13. }
  14. else
  15. {
  16. $scope.words = angular.copy(wordObjects);
  17. }
  18. }
  19.  
  20. $scope.buildSentance = function (w) {
  21.  
  22. var words = [];
  23.  
  24. for (var i = 0; i < $scope.words.length; i++) {
  25. var word = $scope.words[i].word;
  26. if (word.replace(/\s+/g,'') !== '') {
  27. words.push(word);
  28. }
  29. }
  30.  
  31. $scope.sentence = words.join(' ');
  32.  
  33. // if the user puts a space in the input
  34. // call parseSentence() to update $scope.words
  35. if (w.word.indexOf(' ') > -1) {
  36. $scope.parseSentenceDebounced();
  37. }
  38. }
  39. $scope.parseSentence();
  40.  
  41. });

希望这能解决您的问题.

猜你在找的Angularjs相关文章