Hello, I have a problem.

It is necessary to realize that in the same input field both the tag and the message are entered, and then entered into the array as two different objects.

Here is an example where this is implemented in two fields.

var app = angular.module('testApp', []); app.controller('mainCtrl', function($scope){ $scope.todoList = [{ todoText: " " , hashtag: " " }]; $scope.todoAdd = function() { $scope.todoList.push({ hashtag: "#" + $scope.todoHashTag , todoText: $scope.todoMessage }); $scope.todoMessage = ""; $scope.todoHashTag = ""; }; }); 
 <body ng-app="testApp" ng-controller="mainCtrl"> <h2>Test App</h2> <form ng-submit="todoAdd()"> <input id="hashTag" type="text" ng-model="todoHashTag" size="50" placeholder="Tag"> <input id="message" type="text" ng-model="todoMessage" size="50" placeholder="Message"> <input id="butGetNew" type="submit" value="Add New"> <!--add new--> </form> <br> <div ng-repeat="x in todoList"> <span ng-bind="x.hashtag"></span> <span ng-bind="x.todoText"></span> </div> </body> 

  • What character tag? What language is the message? - Vladimir Alexandrov
  • The usual hashtag, as in instagram or twitter. English - shadelete
  • I understand you need to implement that you could write a few hashtags? - Vladimir Alexandrov
  • No, just one. After the space there is a message that is written to a separate array - shadelete

1 answer 1

 если только первое слово является хэштегом то можно так сделать например 

 function cut() { var message = $('#message').val(); var count = message.indexOf(' '); //ищем первый пробел в сообщении var hashtag = message.substring(0, count); //отрезаем первое слово if(hashtag[0] != '#') hashtag = '#' + hashtag; var text = message.substring(count, message.length); //отреаем все что после первого слова console.log('hashtag: ' + hashtag); console.log('message: ' + text); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="message"/> <input type="button" onclick="cut()" value="Порезать"/> 

  • Yes! Exactly what you need! Thank you so much) - shadelete
  • @endoneslife, thanks here is a plus and, if decided, accepting an answer. - user207618