I am trying to replace all line breaks in an object property which is being filled using angular.copy from a json object but for some reason the value can not be modified.
我试图替换使用json对象中的angular.copy填充的对象属性中的所有换行符,但由于某种原因,该值无法修改。
angular.copy(data, vm.candidate);
console.log(vm.candidate.Comments.replace(/\n/g, '<br/>'));
The output is still: hi\nhow\nare\nu\n
输出仍然是:hi \ nhow \ nare \ nu \ n
But if I set this output in a variable
但是,如果我将此输出设置为变量
var xx = "hi\nhow\nare\nu\n";
console.log(xx.replace(/\n/g, '<br/>'));
The output is: hi
how
are
u
as expected.
输出是:你好,你是如何预期的。
Basically, I am trying to do something like this:
基本上,我试图做这样的事情:
vm.candidate.Comments=$sce.trustAsHtml(vm.candidate.Comments.replace(/\n/g, '<br/>'));
In the view:
在视图中:
<p ng-bind-html="candCtrl.candidate.Comments"></p>
Trying to get the right outoput in the view:
试图在视图中获得正确的outoput:
"hi how are u"
“嗨!你好吗”
Any ideas why it is not working?
任何想法为什么它不起作用?
A jsfiddle example: jsfiddle
一个例子:jsfiddle
1 个解决方案
#1
1
*Notice, I'm working with the jsfiddle provided in the comments to the original question.
*注意,我正在使用原始问题的评论中提供的jsfiddle。
Basically, you're using angular.copy
wrong.
基本上,你使用angular.copy错了。
According to the angular docs at here:
根据这里的角度文档:
[angular.copy] Creates a deep copy of source, which should be an object or an array.
[angular.copy]创建源的深层副本,该副本应该是对象或数组。
You're trying to copy a string
. The solution is to copy the entire object and then use your regex to replace \n
with <br/>
你正在尝试复制一个字符串。解决方案是复制整个对象,然后使用正则表达式将\ n替换为\
your code:
function LoginController($scope) {
$scope.car2 = {};
$scope.car = { "ID": 3, "Comments": "hi\\nhow\\nare\\nu\\n","RatingID": 2,"Rating":"Unsure"};
$scope.car2=angular.copy($scope.car.Comments.replace(/\n/g, '<br/>'));
}
working code (jsfiddle):
工作代码(jsfiddle):
function LoginController($scope) {
$scope.car2 = {};
$scope.car = { "ID": 3, "Comments": "hi\nhow\nare\nu\n","RatingID": 2,"Rating":"Unsure"}
$scope.car2 = angular.copy($scope.car)
$scope.car2 = $scope.car2.Comments.replace(/\n/g, '<br/>')
}
#1
1
*Notice, I'm working with the jsfiddle provided in the comments to the original question.
*注意,我正在使用原始问题的评论中提供的jsfiddle。
Basically, you're using angular.copy
wrong.
基本上,你使用angular.copy错了。
According to the angular docs at here:
根据这里的角度文档:
[angular.copy] Creates a deep copy of source, which should be an object or an array.
[angular.copy]创建源的深层副本,该副本应该是对象或数组。
You're trying to copy a string
. The solution is to copy the entire object and then use your regex to replace \n
with <br/>
你正在尝试复制一个字符串。解决方案是复制整个对象,然后使用正则表达式将\ n替换为\
your code:
function LoginController($scope) {
$scope.car2 = {};
$scope.car = { "ID": 3, "Comments": "hi\\nhow\\nare\\nu\\n","RatingID": 2,"Rating":"Unsure"};
$scope.car2=angular.copy($scope.car.Comments.replace(/\n/g, '<br/>'));
}
working code (jsfiddle):
工作代码(jsfiddle):
function LoginController($scope) {
$scope.car2 = {};
$scope.car = { "ID": 3, "Comments": "hi\nhow\nare\nu\n","RatingID": 2,"Rating":"Unsure"}
$scope.car2 = angular.copy($scope.car)
$scope.car2 = $scope.car2.Comments.replace(/\n/g, '<br/>')
}