1

I am new to angularjs. I am working on angularjs controller. Inside a controller I have the below function

$scope.violation = function(argArray){
        var message = '';
        var violationLog = argArray;
        if(violationLog!= null && violationLog!='undefined')
        {
            var violationArray = new Array();
            violationArray = violationLog.split("<br/>");

           for (var i = 1; i < violationArray.length; i=i+2)
            {

                message += violationArray[i];

            }

        }
        return message;
     }

I am passing "argArray" variable to that function as

argArray = "4<br/>Time in this state exceeded 2 minutes.<br/>3<br/>Time in this state exceeded 1 minutes.";

Here, I want to arrange those two messages in 'tr' tags inside a table.

Like,

View Demo here

Help me.

3 Answers 3

2

I'm not a fan of your "array" being an HTML string, but if that is really the case, the best you can do, is to split it up to an actual array and feed that to your repeater:

HTML

<div ng-app="myApp" ng-controller="dynamicTable">

    <div ng-bind-html="violation(argArray)"></div>

</div>

Angular

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

myApp.controller("dynamicTable", function($scope, $sce) {

    $scope.argArray = "4<br/>Time in this state exceeded 2 minutes.<br/>3<br/>Time in this state exceeded 1 minutes.";

    $scope.violation = function(argArray){

            var message = '';
            var html = "<table>";
            var violationLog = argArray;
            if(violationLog!= null && violationLog!='undefined')
            {

                var violationArray = new Array();
                violationArray = violationLog.split("<br/>");
               for (var i = 1; i < violationArray.length; i=i+2)
                {
                    message += "<tr><td>";
                    message += violationArray[i];
                    message += "</td></tr>";
                    html += message;
                }

            }
            html += "</table>";

            return $sce.trustAsHtml(html);
    }
});

Fiddle here: http://jsfiddle.net/5kjjsn3L/6/

UPDATE: This example was originally a table created with a repeater. The OP wanted the HTML created inside the controller, so it has been modified do just that.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for reply. Here also I am getting the same issue. Secong message is appending to the first message. So, I want to split the message using tr, td tags inside the function. Can you please tell me how to write table and tr, td tags inside the function.
Can you update the fiddle to show the issue? Then I'm sure we can find a solution.
I want something like : jsfiddle.net/5kjjsn3L/5
If thats what you want, I have fixed it for you here: jsfiddle.net/5kjjsn3L/6
Good. I have updated the answer with the solution we came up with. Please mark it as an answer, if it meets your requirements.
0

Is this you wanted?

<table>
      <tr ng-repeat="msg in model.messages">
          <td>{{ msg }}</td>
      </tr>

  </table>

detail pnlkr

Comments

0

In your html you create the table like this

<table>
     <tr ng-repeat="msg in messages">
          <td>{{msg}}</td>
     </tr>
</table>

Then in your controller, you push your message in $scope.messages

$scope.violation = function(argArray){
    var violationLog = argArray;
    if(violationLog!= null && violationLog!='undefined')
    {
        var violationArray = new Array();
        violationArray = violationLog.split("<br/>");

       for (var i = 1; i < violationArray.length; i=i+2)
        {
            $scope.messages.push(violationArray[i]); 
        }

    }
 }

1 Comment

Thanks for reply. I want table, tr, td tags inside the function dynamically. Fiddle : jsfiddle.net/5kjjsn3L/5

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.