angularjs - How to replace every instance of a string in a $http json response -
codepen here: http://codepen.io/anon/pen/dgenlk
i have $http response following.
$http({ method: 'get', url: _sppagecontextinfo.webabsoluteurl + "/itemssite/_api/web/lists/getbytitle('items')/items?$select=*", cache: true, headers: { "accept": "application/json;odata=verbose" } }).success(function (data, status, headers, config) { $scope.items = data.d.results; }).error(function (data, status, headers, config) { }); the results, of there between 50 , 100, somewhat this
{ name: 'item one', desc: "item 1 [item:1]view 1[/item] item 1b [item:1b]view 1b[/item] item 1c [item:1c]view 1c[/item]" }, { name: 'item two', desc: "item 2 [item:2]view 2[/item] item 2b [item:2b]view 2b[/item] item 2c [item:2c]view 2c[/item]" }, { name: 'item three', desc: "item 3 [item:3]view 3[/item] item 3b [item:3b]view 3b[/item] item 3c [item:3c]view 1c[/item]" } and displayed using ng-repeat like.
< div ng-repeat="item in items"> < div class="col-sm-12" ng-bind-html="item.details | filter:search">< /div> < /div> inside each block of text potentially contains multiple strings old system resemble following.
[item:id]item name[/item] how transform json instances of above string intercepted , transformed clickable modal link such as
< ng-click="viewitemdetails('1a');" >view 1a< /a > i cannot change structure of text blocks.
note: have asked similar question before , suggestions neither worked nor graspable.
one way pre process data. this: (i'll post link working code soon)
$scope.items.map(function(n) { var str = n.desc, re = /\[item:.*?\]/ig, found = str.match(re); found = found.map(function(m) { return m.split(':')[1].replace(']', ''); }); return n.items = found; }); and use new "items" property in nested ng-repeat:
<ul> <li ng-repeat="item in items" fixlinks>{{item.name}} <div> <span ng-repeat="n in item.items"> item {{n}} <a href ng-click="viewitemdetails({{n}})"> view {{n}} </a> </span> </div> <hr/> </li> </ul> update here's pen replacement:
$scope.items.map(function(n) { var str = n.desc, re = /\[item:(.*?)\]/ig, tpl = '<a href ng-click="$1">' str = str.replace(re, tpl); str = str.replace(/\[\/item\]/ig, '</a>'); return n.mudesc = str; }); update #2 : using directive, compiling , calling controller scope method. can improved helps now. here's updated pen
directive code:
app.directive('fixlinks', function ($parse, $compile) { function replacestr(str) { var re = /\[item:(.*?)\]/ig, tpl = '<a href ng-click="click(\'$1\')">' str = str.replace(re, tpl); str = str.replace(/\[\/item\]/ig, '</a>'); return str; } return { scope: { text: '=', flclick: '&' }, link: function(scope, element) { var el = angular.element('<span>' + replacestr(scope.text) + '</span>'); $compile(el)(scope); element.append(el); scope.click = function(id) { scope.flclick({id: id}); } } }; }); markup:
<ul> <li ng-repeat="item in items"> <div>{{item.name}}</div> <div fix-links text="item.desc" fl-click="viewitemdetails(id)"></div> <hr/> </li> </ul>
Comments
Post a Comment