[英]How to convert a string with an HTML code into a working HTML code (in Ruby or Javascript)?
I am putting following code into a Ruby array: 我将以下代码放入Ruby数组中:
@data << "=> <div><a href='/company/#{d.name}'>See profile</a></div>"
I've tried this: 我已经试过了:
@data << "=> <div><a href='/company/#{d.name}'>See profile</a></div>".html_safe
But it didn't help me. 但这并没有帮助我。
I am passing the @data
to JSON ( @data.to_json
), so then in the JS, I would need to have the label See profile as a link, not a text. 我将@data
传递给JSON( @data.to_json
),因此在JS中,我需要将标签See profile作为链接而不是文本。 So I tried 所以我尝试了
label1 = variable_with_the_string_from_above.html();
But this didn't help me out too... In the JS variable is the See profile still like a plain text only ( <div><a href='/company/#{d.name}'>See profile</a></div>
), not like an HTML link. 但这对我也没有帮助...在JS变量中,“ 查看”个人资料仍然仅像纯文本( <div><a href='/company/#{d.name}'>See profile</a></div>
),而不是HTML链接。
How to do that? 怎么做?
Thank you guys 感谢大伙们
In JavaScript: 在JavaScript中:
var htmlArray = ["<div><a href='/company/#{d.name}'>See profile</a></div>"];
To inject into the HTML: 注入HTML:
document.write(htmlArray[0]);
If you are passing the {d.name} as a variable, try AngularJS. 如果将{d.name}作为变量传递,请尝试AngularJS。
Create a controller and scope -> 创建一个控制器和范围->
App.controller('MainCtrl', ['$scope', function ($scope)
{
$scope.dname = dname(); etc.
}]);
and than in your html or js: 比在您的html或js中:
var htmlArray = ["<div><a href='/company/{{dname}}'>See profile</a></div>"];
Make sure the array is within the range of your controller scope! 确保阵列在您的控制器范围内!
<div ng-controller="MainCtrl">
...
</div>
An easier way to do this would be just to use AngularJS routing: 一种更简单的方法是使用AngularJS路由:
sampleApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/companyOne', {
templateUrl: 'templates/c1.html',
controller: 'c1'
}).
when('/companyTwo', {
templateUrl: 'templates/c2.html',
controller: 'c2'
}).
otherwise({
redirectTo: '/default'
});
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.