javascript - How to make clickable URL's in JSON response from REST using AngularJS? -


i have angularjs, js, jq, html5 web app, can send different http methods project's restful web service , receive responses it. response comes in json , looks this:

enter image description here

this displayed in <pre> element way:

<div ng-controller="responsecontroller">     <span class="status-{{response.status}}">{{response.code}} {{response.description}}</span>     <pre>{{response.data}}</pre>     <p id="responseheaders">response headers:</p>     <table>         <tr  ng-repeat="header in response.headers">             <td><a href="#" ng-click="addtoheaders(header)">{{header.name}}</a></td>             <td><span>{{header.value}}</span></td>         </tr>     </table> </div> 

what want have url's received in json clickable, hang ng-click on them , go on next steps. can't in <pre> element.

any ideas how can solved? every useful answer highly appreciated , evaluated.

thank you.

you can achieve using directive (here's example):

.directive('jsonlink', function() {     var hreflinks=function (obj, pretty) {       var space = pretty || '';       var html = '';       $.each(obj, function(key, val) {         if ($.isplainobject(val)) {           html += key + ': <br>';           html += hreflinks(val, space + '&nbsp;&nbsp;');         } else if (key === 'href') {           html += space + 'link: <a href="' + val + '">' + val + '</a><br>';         } else {           html += space + key + ': ' + val + '<br>';         }       });       return html;     }     return {       restrict: 'e',       template: '<div></div>',       replace: true,       scope: {         obj: '='       },       link: function(scope, element, attr) {         element.html(hreflinks(scope.obj));       }     };   }) 

then drop in html

<json-link obj="objwithlinks"></json-link> 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -