1

How can I insert my html view into the box? From id test1, into the vm.input variable?

angular.js:13920 Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html

Controller:

 vm.showbox = function(name) {
      console.log("testing name " + name);

      var a = angular.element('#' + name)[0];
      console.log(a);
      vm.inputbox =  $sce.trustAsHtml(a);

      console.log(vm.inputbox);
    };

view:

<div class"click"  ng-click="vm.showbox('test1')">click/div>

  <div  id="test1"  class="ng-hide">
        <h3>Shema</h3>
       schema here.
    </div>

     <div  style="display: block;"  ng-bind-html="vm.inputbox ">

        </div>

console log:

 <div  id="test1"  class="ng-hide">
        <h3>Shema</h3>
       schema here.
    </div>

angular.js:13920 Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html
Setily
  • 814
  • 1
  • 9
  • 21
maria
  • 207
  • 5
  • 22
  • 56
  • That's your problem. $sce.trustAsHtml requires a string, and `angular.element` does not return a string, it returns a jQuery object. https://docs.angularjs.org/api/ng/function/angular.element – Pop-A-Stash Sep 27 '16 at 15:45
  • but the angular.element -> [0] returns me the div with id test1. How can i get the div to store it into vm.inputbox or show it somewhere? – maria Sep 27 '16 at 15:46
  • I think that is the subject of a different question: http://stackoverflow.com/questions/8127091/jquery-get-dom-element-as-string – Pop-A-Stash Sep 27 '16 at 15:51
  • Possible duplicate of [AngularJS: How to resolve "Attempting to use an unsafe value in a safe context"?](https://stackoverflow.com/questions/41996899/angularjs-how-to-resolve-attempting-to-use-an-unsafe-value-in-a-safe-context) – Mistalis Jul 12 '17 at 15:16

1 Answers1

1
app.filter('trustFilter', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}]);

You should use filter for it. And made some changes to like

<div style="display: block;" ng-bind-html="inputbox | trustFilter">

And In your controller

vm.inputbox = name;

$sce.trustAs requires a string value. maybe it will help

svarog
  • 9,477
  • 4
  • 61
  • 77
Ujjwal kaushik
  • 1,618
  • 11
  • 17