简体   繁体   中英

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:

@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. 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.

How to do that?

Thank you guys

In JavaScript:

var htmlArray = ["<div><a href='/company/#{d.name}'>See profile</a></div>"];

To inject into the HTML:

document.write(htmlArray[0]);

If you are passing the {d.name} as a variable, try AngularJS.

Create a controller and scope ->

App.controller('MainCtrl', ['$scope', function ($scope) 
{
$scope.dname = dname(); etc. 
 }]);

and than in your html or 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:

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'
      });
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM