angularjsng-bind-htmlng-bind

ng-bind-html values to other html tags


I want to take data from ng-bind-html and bind it to some other html tags.Though i can directly use ng-bind-html for it. But is there any other way where we can use the binded html to other HTML tags?

JS:

 $scope.left = '<span>Hello World</span><strong>New Testing<strong><br/>h1>Testing</h1>';

HTML:

<div ng-bind-html="left" ng-model="GetValues"></div>

<span>{{GetValues}}</span>

Solution

  • Way 1 : Achieve with using $compile

    html

    <div my-directive="left" ng-model="GetValues"></div>
    

    directive

    var app = angular.module('myApp',[]);
    app.controller('ctrlMain',function($scope){
        $scope.bindMe = {id:1,myvar:"test"};
    });
    app.directive('myDirective', function($compile){
      return{
        restrict: 'A',
        scope: {
            varToBind: '=myDirective'     
        },
        link: function(scope, elm, attrs){
          outerList = '<span>Hello World</span><strong>New Testing<strong><br/>h1>Testing</h1>';
          outerList = $compile(outerList)(scope);
          elm.replaceWith(outerList);
        }
      }
    });
    

    way 2: Achieve with using AngularJS ng-include

    you can directly include a html file :)

    <div ng-include="'myFile.htm'"></div>//your DOM string code should be in myfile.htm file