0


I have this function in jQuery but I'm facing problem converting that into angularJs function:

$('p').each(function() {
$(this).html($(this).text().split(/([\.\?!])(?= )/).map(
  function(v){return '<span class=sentence>'+v+'</span>'}
));

It would really help if someone could explain to me how one would implement these lines of code in angularJs
Thanks in advance guys

1
  • You can use directive for this. Commented Dec 5, 2016 at 15:21

2 Answers 2

0

easiest way is just splitting into an array and binding that to your controller.

More in depth way would be to roll your own custom directive, let me know if you want to see a way with a directive

<html ng-app="MyCoolApp">
<head>
</head>
<body ng-controller="MyCoolController">
<p ng-repeat="text in Array">
{{text}}
</P>
</body>
</html>
<script type="text/javascript">
    var myApp = angular.module('MyCoolApp',[]);

    myApp.controller('MyCoolController', ['$scope', function($scope) {
    var dataSource = [];//your data split
      $scope.Array = dataSource;
    }]); 
</script>

edit

Updated to use custom directive. YOu will need to tweak the regEx to split properly plunkr example

split.html

<div>
  <textarea ng-model="input"></textarea>
  <button ng-click="Process(input)">Process</button>
  <p>
    <span style="border:1px solid red" ng-repeat="text in Output track by $index">
      {{text}}
    </span>
  </p>
</div>

script.js

(function(angular) {
  'use strict';
angular.module('splitExample', [])
  .directive('mySplit', function() {
    return {
      templateUrl: 'split.html',
      controller: function($scope){
      $scope.Process = function(text){
            $scope.Output = text.split(/([\.\?!])(?= )/);
            console.log(text)
      }
      }
    };
  })
  })(window.angular);

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-directive-tabs-production</title>


  <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
  <script src="script.js"></script>



</head>
<body ng-app="splitExample">
  <my-split>
</my-split>
</body>
</html>
Sign up to request clarification or add additional context in comments.

6 Comments

It would really help if you showed me this thing done with directive aswell. Basically what my bigger plan with that function is that you have contenteditable area where you can write and copy sentences. But I would want that the angularJs side would add span tags dynamically. And when you one-click on the sentence it highlights it. I know it goes beyond the question I asked here, but I thought I should start from this point where I add span tags to the sentences in the contenteditable area. Hope this makes any sense :jsfiddle.net/HKHGQ basically the same thing as here but in angular
@OskarMartin So you want a directive that once you click a button outputs each sentence with a span tag around it to the DOM?
I maybe explained it a little bit badly. Sorry for my bad english. It would really help if you or someone could show me how to do the same thing as in the jsfiddle link in angular :) jsfiddle.net/HKHGQ
@OskarMartin follow the plunkr link. It does the exact same thing you want, with the exception of creating an alert
Thank you very much for the help :)
|
0

You need to write a directive for it, DOM Manipulation is only allowed in directives in the link function otherwise it is a very bad practice. Here is the code inside link function of directive.

    link: function (scope, element, attributes) {
              var el= $(element);
              el.find('p').each(function(){
                 el.html(el.text().split(/([\.\?!])(?= )/).map(
                    function(value){
                      return '<span class=sentence>'+value+'</span>'
                    }
                 ));                 
              });
           }

Hope it helps you, I am unable to know actually what you are trying to do.. otherwise i would have helped you with full solution and proper directive approach, for any further assistance let me know. Thanks.

Just giving my suggestion : JQuery approach is incompatible with AngularJS.


Updated directive as per your requirement:

angular.module('yourAppModuleName').directive('contentClick', ['$timeout', function($timeout) {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            contentEditable: '=contentEditable'
        },
        link: function(scope, element, attributes) {

            scope.$watch(attributes.contentEditable, function(value) {
                if (value === undefined)
                    return;

                scope.contentEditable = attributes.contentEditable;
            });

            if(scope.contentEditable == 'true') {
             scope.$watch(attributes.ngModel, function(value) {
                if (value === undefined)
                    return;

                value.split(/([\.\?!])(?= )/).map(
                 function(v){
                  return '<span onclick="myFunction(this)" >'+v+'</span>'
                 }
                );                 
             });
            }

            //without timeout
            //function myFunction(x) { 
            //    x.style.background="#000000"; 
            //}  

            //with timeout
            function myFunction(x) { 
                $timeout(function() {
                  x.style.background = "green"; 
                }, 10000); 
            }          
        }
    };
}]);

HTML:

<div contentEditable="isEditable" style="cursor:pointer" ng-model="content" content-click></div>

Controller:

yourAppModuleName.controller('myController', ['$scope', function($scope) {

      $scope.isEditable = true;
      $scope.content;

}]);

4 Comments

Basically my idea is that I have div that has the parameter contenteditable=true. You can write in this div and when you one-click the sentence it gives the sentence you clicked a background-color in css(highlights the text). I'm pretty new to angular so I'm not sure how to implement it in code. As I understand I need to use directive for that? It would really help if you could guide me through this :)
<div contentEditable="true" onclick="myFunction(this)" style="cursor:pointer"></div> <script> function myFunction(x) { x.style.background="#000000"; } </script> You can setTimeout also if you want background color change should visible for few minutes function myFunction(x) { setTimeout(function(){ x.style.background = "yellow"; }, 10000); } here i have given timeout of 1 minute
And does this solution here cover it in angular?
No, this is basic javascript, but can be implemented in angular, as i have updated my answer for you.. but instead of doing that you can use javascript as its simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.