1

When I run this code on my PHP website, it works but when adding a \n after 'text:' as php to make a new line, the entries in the table below the blue title at the top stop showing. Below is a snippet of what the code is but it does not run php so it doesn't show the error.

function MyCtrl($scope) {
  $scope.environment_service_packages = 
    [
      {name: 'obj1', info: {text: '<?php echo "This: \n breaks the code"; ?>', 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>

2 Answers 2

2

You actually need to escape the line-break twice. One escape for JavaScript and one for php

Your current string generates this php:

<?php echo "This: 
breaks the code"; ?>

because the \n is evaluated by javascript into a linefeed character and this makes the php invalid.

You should use this string instead:

'<?php echo "This: \\n does not break the code"; ?>'
Sign up to request clarification or add additional context in comments.

Comments

2

\n will not show in HTML. You have to coding like this.

function MyCtrl($scope) {
   $scope.environment_service_packages = 
[
  // &lt;br&gt; will be convert to <br>
  {name: 'obj1', info: {text: '<?php echo "This: &lt;br&gt; breaks the code"; ?>', show: true}}, 
  {name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
];
}

Comments

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.