1

This code below uses Javascript and PHP, but when it runs I want each echo to be on a separate line. I have tried using \n and <br> and other methods but all of them dont have any effect on the text. Can anyone please help me?

function MyCtrl($scope) {
  $scope.environment_service_packages = 
    [
      {name: 'obj1', info: {text: '<?php echo "hello" . "\n" . "<br>" . "world"; ?>', show: true}},
      {name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
    ];
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app>
  <table ng-controller="MyCtrl" class="table table-hover table-striped">
    <tr class="info">
      <td>...</td>
    </tr>
    <tbody ng-repeat="x in environment_service_packages">
      <tr ng-click="x.info.show = !x.info.show">
        <td> {{ x.name }}
      </tr>
      <tr ng-show="x.info.show">
        <td>
          {{ x.info.text }}
        </td>
      </tr>
    </tbody>
  </table>
</body>

Edit: For some reason this snippet does not interpret the php.

13
  • I can't run your code -- it's full of undefined variables. Please see this page -- minimal reproducible example -- and give us an example that only has the problem you want us to address. Commented Jul 3, 2016 at 2:01
  • @TopologicalSort Ok, I have updated the question. Commented Jul 3, 2016 at 3:22
  • Your <br> should work in this case. You can do a small proof by writing your content manually in the html. Why (what's the effect) do you say they didn't work? Did they render as text? If so, it is probably your framework or engine escaping it to output a text form. Or a CSS thing? Hard to say without a minimal complete example. However, br tags inside a td do produce a newline :) Commented Jul 3, 2016 at 3:35
  • Try <td ng-bind-html="x.info.text"></td> BTW - The whole idea of angularjs is NOT to have PHP injected in the client side.......... Commented Jul 3, 2016 at 3:36
  • @AlonEitan Doesn't matter. Where should I put that code? Commented Jul 3, 2016 at 3:41

2 Answers 2

2

You just need to put the expression between <pre></pre> tags:

<td>
    <pre>{{ x.info.text }}</pre>
</td>

And then use \n as a line break (Forget the <br>).

Here's a working example. I removed the PHP tags and used plain text for the demo:

text: 'line 1\nline 2'

BTW - You can replace the PRE tags with a white-space: pre; css style.

function MyCtrl($scope) {
  $scope.environment_service_packages = 
    [
      {name: 'obj1', info: {text: 'line 1\nline 2', show: true}},
      {name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
    ];
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app>
  <table ng-controller="MyCtrl" class="table table-hover table-striped">
    <tr class="info">
      <td>...</td>
    </tr>
    <tbody ng-repeat="x in environment_service_packages">
      <tr ng-click="x.info.show = !x.info.show">
        <td> {{ x.name }}
      </tr>
      <tr ng-show="x.info.show">
        <td>
          <pre>{{ x.info.text }}</pre>
        </td>
      </tr>
    </tbody>
  </table>
</body>

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

12 Comments

Thank you for this!
Didn't think about the pre. You're right. Otherwise represent text by array and nest ng-repeat in the <td> tag
I did upvoted your answer, because it provide an alternative to my solution. Nice chatting with you ;)
For some reason, the php works until i add the \n then it just shows the top blue one and does not show the boxes below it.
@Jez Try working with json_encode: text: <?php echo json_encode("hello\n" . $myVar); ?> - Note that you should NOT wrap the what should be the property value with any quotes. This will explain what i'm talking about: echo '{text: ' . json_encode('value with " and \' chars') . '}';
|
2

The ng-bindings do not interpret html entities therefore you have to represent your echoes a such :

function MyCtrl($scope) {
  $scope.environment_service_packages = 
    [
      {name: 'obj1', info: {text:['<?php echo "hello" ?>','<?php echo  "world"; ?>'], show: true}},
      {name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
    ];
}




<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app>
  <table ng-controller="MyCtrl" class="table table-hover table-striped">
    <tr class="info">
      <td>...</td>
    </tr>
    <tbody ng-repeat="x in environment_service_packages">
      <tr ng-click="x.info.show = !x.info.show">
        <td> {{ x.name }} </td>
      </tr>
      <tr ng-show="x.info.show">
        <td>
          <p ng-repeat="txt in x.info.text">{{ txt }}</p>
        </td>
      </tr>
    </tbody>
  </table>
</body>

Your first <td> tag is not closed.

<tr ng-click="x.info.show = !x.info.show">
    <td> {{ x.name }} </td>
</tr>
<tr ng-show="x.info.show">
   <td>
      <p ng-repeat="txt in x.info.text">{{ txt }}</p>
   </td>
</tr>

By the way, It's bad practice to echo PHP in your Angular code.

6 Comments

I think that the browser will know how to handle this, by auto closing the tag when it gets to the </tr> wrapping the cell with the missing closing </td> tag
Then there is no reason a <br> inside à table data cell does not break line.
But the <br> tag is on the second cell, and it has a closing </td> tag
Yes, but still, some upper html code could break the rest. I've tested the table on chrome and the <br> tag does work even if the <td> before isn't closed. So you're right, but it's the only thing I can see that could prevent it from working.
The reason is that ngBind (which is the equivalent of {{ expression }}) doesn't work with html entities: Reference The ngBind attribute tells Angular to replace the text content of the specified HTML element with the value of a given expression The OP is using PHP inside their controller - This is something i've never seen before... Making it pretty bad practice as a whole
|

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.