下面的代码使用 Javascript 和 PHP,但当它运行时,我希望每个 echo 都在单独的一行上。我尝试过使用\n 和
以及其他方法,但它们都对文本没有任何影响。谁能帮帮我?
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>
编辑:出于某种原因,此代码段不解释 php。
最佳答案
您只需要将表达式放在<pre></pre>
之间标签:
<td>
<pre>{{ x.info.text }}</pre>
</td>
然后使用\n
作为换行符(忘记 <br>
)。
这是一个工作示例。我删除了 PHP 标签并使用纯文本进行演示:
text: 'line 1\nline 2'
顺便说一句 - 您可以替换 PRE
带有 white-space: pre;
的标签CSS 样式。
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>
关于javascript - 在 PHP 中回显时,新行在 Javascript 函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165677/