Skip to content Skip to sidebar Skip to footer

Replace Html Tag Attribute In A String Using AngularJS

I have a string containing html tags : var str = '

blabla

blaaaaaablaaaaa<

Solution 1:

See this fiddle

As you are using a string to generate angular element, You can simply replace the <iframe> with <button> in the string itself.

Example:

$scope.str='<iframe>some iframe text<span>span content</span><span>more span content</span></iframe>';

$scope.newStr= $scope.str.replace('<iframe>','<button>').
replace('</iframe>','</button>')

Solution 2:

Finally, here the solution I found myself :

function editFrameAttributes(str,newSrc){
      var eles = angular.element("<div>"+str+"</div>");
      eles.find('[src]').each(function() {
        this.src = newSrc;
      });
      return eles.html();
    }

Post a Comment for "Replace Html Tag Attribute In A String Using AngularJS"